Free-ing memory problem. C/C++, free()
Here is the code, which just creates a dynamic array? fills it? and prints.
At the end-point? when doing 'free(digits);' I get a debug messge. But the programm continues to work, or it crashes only on free? How do I need to change the source-code, not to recieve a message from debugger? Sorry for bad English. Thanks in advance. SOURCE: #include <stdio.h> #include <stdlib.h> using namespace std; int main() { unsigned int N; unsigned int M; int *digits; unsigned int i=0; unsigned int k=0; printf("\n Type in the number of Numbers: N="); scanf("%u", &N); printf(" Type in the number of moves: M="); scanf("%u", &M); digits = (int *)(calloc(M, sizeof(int))); for(i=0; i<N; i++) { printf(" Type in the %u-th digit: D=", i); scanf("%d", &digits[i]); } printf("You see following digits:"); for (k=0, i=M; k<10; k++, i++) { i=i%N; printf(" %u", digits[i]); } free(digits); return 0; } WHAT I GET: [sergey@localhost:Debug]$ ./Wonder Type in the number of Numbers: N=12 Type in the number of moves: M=7 Type in the 0-th digit: D=1 Type in the 1-th digit: D=2 Type in the 2-th digit: D=3 Type in the 3-th digit: D=4 Type in the 4-th digit: D=5 Type in the 5-th digit: D=6 Type in the 6-th digit: D=7 Type in the 7-th digit: D=8 Type in the 8-th digit: D=9 Type in the 9-th digit: D=0 Type in the 10-th digit: D=7 Type in the 11-th digit: D=7 *** glibc detected *** ./Wonder: free(): invalid next size (fast): 0x0804a008 *** ======= Backtrace: ========= /lib/i686/cmov/libc.so.6[0xb7658624] /lib/i686/cmov/libc.so.6(cfree+0x96)[0xb765a826] ./Wonder(__gxx_personality_v0+0x23f)[0x80488db] /lib/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7600455] ./Wonder(__gxx_personality_v0+0x35)[0x80486d1] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:05 810018 /home/sergey/workspace/Wonder/Debug/Wonder 08049000-0804a000 rw-p 00000000 08:05 810018 /home/sergey/workspace/Wonder/Debug/Wonder 0804a000-0806b000 rw-p 00000000 00:00 0 [heap] b7400000-b7421000 rw-p 00000000 00:00 0 b7421000-b7500000 ---p 00000000 00:00 0 b75e9000-b75ea000 rw-p 00000000 00:00 0 b75ea000-b773f000 r-xp 00000000 08:06 312640 /lib/i686/cmov/libc-2.7.so b773f000-b7740000 r--p 00155000 08:06 312640 /lib/i686/cmov/libc-2.7.so b7740000-b7742000 rw-p 00156000 08:06 312640 /lib/i686/cmov/libc-2.7.so b7742000-b7745000 rw-p 00000000 00:00 0 b7745000-b7751000 r-xp 00000000 08:06 303148 /lib/libgcc_s.so.1 b7751000-b7752000 rw-p 0000b000 08:06 303148 /lib/libgcc_s.so.1 b7752000-b7753000 rw-p 00000000 00:00 0 b7753000-b7777000 r-xp 00000000 08:06 312644 /lib/i686/cmov/libm-2.7.so b7777000-b7779000 rw-p 00023000 08:06 312644 /lib/i686/cmov/libm-2.7.so b7779000-b785c000 r-xp 00000000 08:06 1746050 /usr/lib/libstdc++.so.6.0.10 b785c000-b785f000 r--p 000e2000 08:06 1746050 /usr/lib/libstdc++.so.6.0.10 b785f000-b7861000 rw-p 000e5000 08:06 1746050 /usr/lib/libstdc++.so.6.0.10 b7861000-b7867000 rw-p 00000000 00:00 0 b788b000-b788f000 rw-p 00000000 00:00 0 b788f000-b7890000 r-xp 00000000 00:00 0 [vdso] b7890000-b78aa000 r-xp 00000000 08:06 303106 /lib/ld-2.7.so b78aa000-b78ac000 rw-p 0001a000 08:06 303106 /lib/ld-2.7.so bfe1e000-bfe33000 rw-p 00000000 00:00 0 [stack] You see following digits: 8 9 0 7 7 1 2 3 4 5Аварийный останов "Аварийный останов" -- is "Emergency stop" but in Russian. (I'm russian). |
Let's see - you're reading 12 items into a 7 element array?
That can't be good ;-) |
Thanks a lot!
I am sorry for I disturbed you for such a little mistake... |
paulsm4 explained why the program is wrong.
More specifically, you have Code:
digits = (int *)(calloc(M, sizeof(int))); Code:
digits = (int *)(calloc(N, sizeof(int))); Quote:
That is typical behavior in C for writing past the end of an array. There are usually no symptoms when you write past the end. The symptoms occur when some later code uses the memory that happens to lie just past the end of the array for whatever the original purpose of that memory had been. |
Quote:
Once upon a time, I used this fact... But it is really risky. And again I am sorry for I disturbed you for such a little mistake. |
It looks like you're beginning to learn programming, so just be aware that C and C++ are _DIFFERENTS_, it looks like you're writing code in C, so founding "using namespace std;" in a C code looks strange, for me at least...
|
@jf.argentino
Not really beginning to learn C. but I've never used C++ syntax yet. |
All times are GMT -5. The time now is 04:38 PM. |