View previous topic :: View next topic
|
Author |
Message |
DIPANKAR SAHA
New User
Joined: 15 Mar 2018 Posts: 1 Location: India
|
|
|
|
My C program is failing because of memory abend. Can i print the call stack at the time of failure like which function asked the memory that cause the fail ? |
|
Back to top |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1312 Location: Vilnius, Lithuania
|
|
|
|
Since when can memory abend? You're on a forum for experts, tell us what really happened and don't invent fake descriptions! |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10884 Location: italy
|
|
|
|
if instead of a generic whining You had posted the messages received
it would have been easier for us to answer.
a C program abending on a malloc/free is a badly written program ...
a malloc returns the address of the malloced storage/memory
after a malloc the program should check for a not NULL value
and behave accordingly
before issuing a free a well behaved program should check for a non NULL storage/memory pointer
here is a snippet to show what I am talking about
Code: |
#include <inttypes.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main()
{
uint8_t *area ;
int areasize = 16*1024*1024 ;
uint8_t *null ;
printf( "***** start of test *****\n");
printf( "before malloc a %d area\n", areasize );
area = malloc( areasize );
if (area == NULL)
{
fprintf (stderr,
"Cannot obtain a %d area: %s\n",
areasize, strerror(errno));
exit(1);
}
printf( "before free a %d area\n", areasize );
if (area != NULL)
free( area );
/* area = NULL; */
printf( "***** end of test *****\n");
free( area );
exit( 0) ;
}
|
the free after the end of test was added to show what happens
when trying to free an invalid storage/memory pointer
Code: |
***** start of test *****
before malloc a 16777216 area
before free a 16777216 area
***** end of test *****
a.out(91122,0x7fffa2b75340) malloc: *** error for object 0x10eee6000: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
|
BUT if You uncomment the area = NULL statement
free will not complain and just ignore the free
from the free man page
Quote: |
The free() function deallocates the memory allocation pointed to by ptr.
If ptr is a NULL pointer, no operation is performed. |
summarizing the whole shebang
ALWAYS CHECK THE RETURNED POINTER FOR NULL
AFTER A FREE ALWAYS SET THE POINTER TO NULL |
|
Back to top |
|
|
|