By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

memprof errors

P: n/a
I am trying to use memprof-0.5.1(because apt-get memprof gives me this
version, although I know version 0.6 came out recently) to debug my
program. My Linux box is a ubuntu, here I list its
kernel/glibc/linker/compiler info
--------------------------------------------------
uname -a
=Linux 2.6.16.27-gg1-em64t #1 SMP
getconf GNU_GLIBC_VERSION
=>glibc 2.3.5
ld --version
=>GNU ld version 2.16.1 Debian GNU/Linux
Copyright 2005 Free Software Foundation, Inc
gcc --version
=>gcc (GCC) 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
memprof --version
=>memprof 0.5.1
--------------------------------------------------

When I test this following simple case by
#gcc -o mytest -g main.c
#memprof mytest
The system hang there when I select File->Run Program->(select mytest),
and also an error message popped up in the console:
*** glibc detected *** free(): invalid pointer: 0x4e401580 ***

-----------------main.c------------------------
#include <stdlib.h>
#include <stdio.h>
void foo(){
int i = 0, j = 8, k = 0;
for(i = 0 ; i < 20; i ++){
malloc(100);
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
printf("k = %d\n", k);
}
}

int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}

----------end of main.c -----------------

Oct 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Sean wrote:
When I test this following simple case by
#gcc -o mytest -g main.c
#memprof mytest
The system hang there when I select File->Run Program->(select mytest),
and also an error message popped up in the console:
*** glibc detected *** free(): invalid pointer: 0x4e401580 ***

-----------------main.c------------------------
#include <stdlib.h>
#include <stdio.h>
void foo(){
int i = 0, j = 8, k = 0;
for(i = 0 ; i < 20; i ++){
malloc(100);
Which pointer points to the newly allocated memory? And what do you
want to do if malloc() fails?
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
printf("k = %d\n", k);
}
}

int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}

----------end of main.c -----------------
You allocated lots of memory(nearly 20M), but why don't you free them
at the end?
If you can answer those questions, you will know why memprof complains.

Oct 3 '06 #2

P: n/a

Cong Wang wrote:
Sean wrote:
When I test this following simple case by
#gcc -o mytest -g main.c
#memprof mytest
The system hang there when I select File->Run Program->(select mytest),
and also an error message popped up in the console:
*** glibc detected *** free(): invalid pointer: 0x4e401580 ***

-----------------main.c------------------------
#include <stdlib.h>
#include <stdio.h>
void foo(){
int i = 0, j = 8, k = 0;
for(i = 0 ; i < 20; i ++){
malloc(100);

Which pointer points to the newly allocated memory? And what do you
want to do if malloc() fails?
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
printf("k = %d\n", k);
}
}

int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}

----------end of main.c -----------------

You allocated lots of memory(nearly 20M), but why don't you free them
at the end?
If you can answer those questions, you will know why memprof complains.
I tried to use this to find memory leakage (well, foo() in this program
is such a case).
Actually, even I free the memory as follows, the memprof hangs there as
is.

Thanks,

--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

void foo(){
int i = 0, j = 8, k = 0;
char* p;
for(i = 0 ; i < 20; i ++){
p = malloc(100);
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
sleep(1);
printf("k = %d\n", k);
free(p);
}
}

int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}
------------------------------------------------

Oct 3 '06 #3

P: n/a

Sean wrote:
Cong Wang wrote:
Sean wrote:
When I test this following simple case by
#gcc -o mytest -g main.c
#memprof mytest
The system hang there when I select File->Run Program->(select mytest),
and also an error message popped up in the console:
*** glibc detected *** free(): invalid pointer: 0x4e401580 ***
>
-----------------main.c------------------------
#include <stdlib.h>
#include <stdio.h>
void foo(){
int i = 0, j = 8, k = 0;
for(i = 0 ; i < 20; i ++){
malloc(100);
Which pointer points to the newly allocated memory? And what do you
want to do if malloc() fails?
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
printf("k = %d\n", k);
}
}
>
int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}
>
----------end of main.c -----------------
You allocated lots of memory(nearly 20M), but why don't you free them
at the end?
If you can answer those questions, you will know why memprof complains.
I tried to use this to find memory leakage (well, foo() in this program
is such a case).
Actually, even I free the memory as follows, the memprof hangs there as
is.

Thanks,

--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>

void foo(){
int i = 0, j = 8, k = 0;
char* p;
for(i = 0 ; i < 20; i ++){
p = malloc(100);
k = j;
while(j < 2*k){
j++;
}
j = 2*k;
sleep(1);
printf("k = %d\n", k);
free(p);
}
}

int main(){
int i = 0, j =2;
for(i = 0; i < 10; i++){
j = j * 2;
foo();
}
}
------------------------------------------------
Again, you forgot to check whether the return value of malloc() is
non-NULL.

And I use valgrind to check your program, it says:

ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 12 from 1)
malloc/free: in use at exit: 0 bytes in 0 blocks.
malloc/free: 200 allocs, 200 frees, 20,000 bytes allocated.
For counts of detected errors, rerun with: -v
No malloc'd blocks -- no leaks are possible.

;-p

Oct 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.