我收到以下類型的錯誤。我知道這跟我不適當地訪問內存有關,但我不完全如此。請幫我看看我哪裏出了問題。爲什麼我得到這個內存訪問錯誤'雙重免費或腐敗'?
*注意我已經簡化了我的函數,這些變量在做什麼並不明顯,我只需要知道我是如何錯誤地實現函數的,或者我濫用了內存訪問。
int my_function(char const *file_name, size_t max)
{
myStruct.pStore = fopen(file_name,"w+"); //pStore is a FILE*
myStruct.max = max;
// fill the with zeros ('0')
int numberOfZeros = max*SIZE;
char zeros[numberOfZeros];
int i=0;
while(i<numberOfZeros) // insert zero's
{
zeros[i]='0';
i++;
}
fwrite(zeros,sizeof(char),numberOfZeros,myStruct.pStore);
fclose(myStruct.pStore);
return EXIT_SUCCESS;
我給出的錯誤:
*** glibc detected *** /home/.../: double free or corruption (top): 0x0804c008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb7e82e42]
/lib/i386-linux-gnu/libc.so.6(fclose+0x154)[0xb7e72384]
/home/2012/spatar/cs/specs/release[0x80486b0]
/home/2012/spatar/cs/specs/release[0x8048acd]
/home/2012/spatar/cs/specs/release[0x8048af0]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e284d3]
/home/2012/spatar/cs/specs/release[0x80484e1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 00:3b 2331829 /home/2012/spatar/cs/Aspecs/release
0804a000-0804b000 r--p 00001000 00:3b 2331829 /home/2012/spatar/cs/specs/release
0804b000-0804c000 rw-p 00002000 00:3b 2331829 /home/2012/spatar/cs/specs/release
0804c000-0806d000 rw-p 00000000 00:00 0 [heap]
b7e0e000-b7e0f000 rw-p 00000000 00:00 0
b7e0f000-b7fae000 r-xp 00000000 00:11 5415 /lib/i386-linux-gnu/libc-2.15.so
b7fae000-b7fb0000 r--p 0019f000 00:11 5415 /lib/i386-linux-gnu/libc-2.15.so
b7fb0000-b7fb1000 rw-p 001a1000 00:11 5415 /lib/i386-linux-gnu/libc-2.15.so
b7fb1000-b7fb4000 rw-p 00000000 00:00 0
b7fbc000-b7fd8000 r-xp 00000000 00:11 5426 /lib/i386-linux-gnu/libgcc_s.so.1
b7fd8000-b7fd9000 r--p 0001b000 00:11 5426 /lib/i386-linux-gnu/libgcc_s.so.1
b7fd9000-b7fda000 rw-p 0001c000 00:11 5426 /lib/i386-linux-gnu/libgcc_s.so.1
b7fda000-b7fdd000 rw-p 00000000 00:00 0
b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
b7fde000-b7ffe000 r-xp 00000000 00:11 5405 /lib/i386-linux-gnu/ld-2.15.so
b7ffe000-b7fff000 r--p 0001f000 00:11 5405 /lib/i386-linux-gnu/ld-2.15.so
b7fff000-b8000000 rw-p 00020000 00:11 5405 /lib/i386-linux-gnu/ld-2.15.so
bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]
顯示釋放內存的部分代碼。 –
使用調試器。使用調試器。使用調試器。 –
'雙倍免費'最初讓我問起同樣的事情 - 但看起來他通過return語句顯示了整個函數。 – aednichols