LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2010, 10:34 AM   #1
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Rep: Reputation: 20
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@localhostebug]$ ./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).
 
Old 01-29-2010, 10:55 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Let's see - you're reading 12 items into a 7 element array?

That can't be good ;-)
 
1 members found this post helpful.
Old 01-29-2010, 11:34 AM   #3
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Original Poster
Rep: Reputation: 20
Thanks a lot!
I am sorry for I disturbed you for such a little mistake...
 
Old 01-29-2010, 11:35 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194Reputation: 1194
paulsm4 explained why the program is wrong.

More specifically, you have
Code:
digits = (int *)(calloc(M, sizeof(int)));
where you should have had
Code:
digits = (int *)(calloc(N, sizeof(int)));
Quote:
Originally Posted by sevs View Post
the programm continues to work, or it crashes only on free?
You might also be confused about why the program continues to work after the point that it writes to the 8'th through 12'th elements of a 7 element array.

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.
 
1 members found this post helpful.
Old 01-29-2010, 11:43 AM   #5
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Original Poster
Rep: Reputation: 20
Quote:
You might also be confused about why the program continues to work after the point that it writes to the 8'th through 12'th elements of a 7 element array.
I've understand you.
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.
 
Old 01-29-2010, 02:20 PM   #6
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 493

Rep: Reputation: 50
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...
 
Old 03-13-2010, 01:31 PM   #7
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Original Poster
Rep: Reputation: 20
@jf.argentino
Not really beginning to learn C. but I've never used C++ syntax yet.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Memory leak: How risky not to free allocated memory. kaz2100 Linux - General 1 12-24-2008 01:00 AM
free memory confusion problem kpachopoulos Linux - Newbie 5 03-31-2008 04:59 PM
LXer: Free Me: a DVD about free culture and free software LXer Syndicated Linux News 0 03-12-2007 01:01 PM
Does free( ) immediatly free the memory? Rayven Programming 24 11-06-2006 03:24 AM
Memory free problem edreddy Programming 1 11-09-2002 11:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration