मैं एक संपीड़न एल्गोरिथ्म को लागू किया है (Huffman कोडिंग का प्रयोग करके) जो नोड्स के एक प्राथमिकता कतार (एक struct मैं परिभाषित) का उपयोग करता है में ठीक काम करता है। अब, जब मैं सिर्फ लिनक्स में या दृश्य स्टूडियो में कोड चलाता हूं, तो सब कुछ ठीक काम करता है। जब मैं विजुअल स्टूडियो में मेमोरी लीक की जांच करता हूं, तो कोई भी नहीं दिया जाता है।वेलग्रिंड: आकार 4 की अवैध पढ़ा -> SIGSEGV, valgrind बिना और दृश्य स्टूडियो
समस्या अब है, जब मैं अपने प्रोग्राम का विश्लेषण करने के लिए valgrind का उपयोग करता हूं, यह सिग्नल 11 (sigsegv) के साथ समाप्त होता है। पहली त्रुटि का सामना मिनट में हटाए गए विधि में 'आकार 4 का अमान्य पढ़ने' है। इसके बाद अन्य त्रुटियां हैं: एड्रेस 453 मुक्त आकार के ब्लॉक के अंदर 0 बाइट्स है, आकार 4 का अमान्य लेखन, अमान्य मुफ्त, हटाएं या फिर से चलाएं।
किसी को भी मुझे मैं संभवतः त्रुटि की किस तरह कर सकता था के बारे में सलाह दे सकते हैं? मैं घंटों तक इंटरनेट खोज रहा हूं, लेकिन मुझे यह नहीं मिल रहा है कि मैं क्या कर रहा हूं (विशेष रूप से जब यह वाल्ग्रिंड का उपयोग नहीं करता है)। या युक्तियाँ कैसे डिबग करें और पता लगाएं कि पढ़ने में त्रुटि क्या हो रही है।
कई धन्यवाद!
अगर कोई व्यक्ति इसकी समीक्षा करना चाहेगा तो कोड यहां है, लेकिन मुझे लगता है कि इस विशिष्ट कोड में केवल गोता लगाने के लिए इतना आसान नहीं है। > हर बार 2 कम से कम नोड्स हटा सकते हैं और दोनों का योग वापस जोड़ने के रूप में -
हिस्सा है जहाँ मैं Huffman हिस्सा कार्य करें:
मेरा अनुमान है कि यह कोड की प्राथमिकता कतार के साथ कुछ है एक नोड
void insert_node(priority_queue* p_queue, node* x){
int i = p_queue->size;
if(i == 0){
p_queue->queue = (node**) malloc(sizeof(node*));
}
else{
p_queue->queue = (node**) realloc(p_queue->queue,sizeof(node*)*(p_queue->size+1));
}
p_queue->queue[p_queue->size] = x;
while(i>=0 && p_queue->queue[i]->amount < p_queue->queue[(i-1)/2]->amount){
node* temp = p_queue->queue[i];
p_queue->queue[i] = p_queue->queue[(i-1)/2];
p_queue->queue[(i-1)/2] = temp;
i = (i-1)/2;
}
p_queue->size++;
}
node* delete_min(priority_queue* p_queue){
node** queue = p_queue->queue;
node* min = queue[0];
if(p_queue->size>1){
int r = 0;
int current = 1; //left child of root
queue[0] = queue[p_queue->size-1];
queue = (node**) realloc(queue,sizeof(node*)*(--p_queue->size));
while(current < p_queue->size){
//in case of 2 children, check if current needs to be right or left child
if(current < p_queue->size-1 && queue[current] > queue[current+1]){
current++;
}
if(queue[current] < queue[r]){
node* temp = queue[r];
queue[r] = queue[current];
queue[current] = temp;
r = current;
current = 2 * current;
}
else{
break;
}
current++;
}
}
else{
free(queue);
p_queue->size--;
}
return min;
}
संपादित करें:
while(queue->size > 1){
node* n1 = delete_min(queue);
node* n2 = delete_min(queue); // all the errors are encountered in this call
node* temp = (node*) calloc(sizeof(node),1);
temp->amount = n1->amount + n2->amount;
insert_node(queue,temp);
n1->parent = temp;
n2->parent = temp;
temp->left = n1;
temp->right = n2;
}
यहाँ है प्राथमिकता कतार के लिए delete_min और insert_node तरीके हैं जोड़ा valgrind उत्पादन:
Invalid read of size 4
==1893== at 0x80498E0: delete_min (huffman.c:331)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893== Address 0x441d9a8 is 0 bytes inside a block of size 452 free'd
==1893== at 0x402BC70: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==1893== by 0x8049922: delete_min (huffman.c:335)
==1893== by 0x80492CC: huffman_encode (huffman.c:195)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893==
==1893== Invalid read of size 4
==1893== at 0x8049901: delete_min (huffman.c:333)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893== Address 0x441db64 is 444 bytes inside a block of size 452 free'd
==1893== at 0x402BC70: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==1893== by 0x8049922: delete_min (huffman.c:335)
==1893== by 0x80492CC: huffman_encode (huffman.c:195)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893==
==1893== Invalid write of size 4
==1893== at 0x8049906: delete_min (huffman.c:333)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893== Address 0x441d9a8 is 0 bytes inside a block of size 452 free'd
==1893== at 0x402BC70: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==1893== by 0x8049922: delete_min (huffman.c:335)
==1893== by 0x80492CC: huffman_encode (huffman.c:195)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893==
==1893== Invalid free()/delete/delete[]/realloc()
==1893== at 0x402BC70: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==1893== by 0x8049922: delete_min (huffman.c:335)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893== Address 0x441d9a8 is 0 bytes inside a block of size 452 free'd
==1893== at 0x402BC70: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==1893== by 0x8049922: delete_min (huffman.c:335)
==1893== by 0x80492CC: huffman_encode (huffman.c:195)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893==
==1893== Invalid read of size 4
==1893== at 0x8049A0E: delete_min (huffman.c:337)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
==1893== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==1893==
==1893==
==1893== Process terminating with default action of signal 11 (SIGSEGV)
==1893== Access not within mapped region at address 0x0
==1893== at 0x8049A0E: delete_min (huffman.c:337)
==1893== by 0x80492DA: huffman_encode (huffman.c:196)
==1893== by 0x8049DDE: encode_file (main.c:94)
==1893== by 0x8049BBE: main (main.c:32)
लाइन 331 की delete_min में लाइन है: नोड * मिनट = कतार [0];
संपादित करें:
समस्या अब हल हो गया है। स्वीकार्य उत्तर में, कारण क्यों समझाया गया है। हटाए गए मान को सही ढंग से असाइन करना, delete_min में सभी मुद्दों को हल किया गया।
//realloc queue and assign new value to local queue var
p_queue->queue = (node**) realloc(queue,sizeof(node*)*(--p_queue->grootte));
queue = p_queue->queue;
'जबकि ((2 * i) +2 < p_queue-> आकार -1 1 <- आप जल्दी बंद हो रहे हैं। यदि' 2 * i + 2 == आकार -1 ', बायां बच्चा अभी भी कतार में मौजूद है, लेकिन आप इसे जांचें नहीं। –
delete_min में कोड वास्तव में स्पष्ट नहीं था, इसलिए मैं इसे और अधिक स्पष्ट करने के लिए पुनः लिखता हूं (नया कोड देखें)। प्रोग्राम का उपयोग करने से अभी भी काम करता है, लेकिन इसे valgrind के साथ जांचते समय, वही त्रुटि अभी भी दिखाई दे रहा है। मुझे अभी भी यह नहीं पता कि यह क्यों चल रहा है, लेकिन यह वाल्ग्रिंड का उपयोग करते समय विफल रहता है। – HaS