| Author |
Message |
|
|
Post subject: Malloc Problem
Posted: Feb 08, 2006 - 07:15 AM
|
|
New Member
Joined: Aug 18, 2005
Posts: 3
|
|
I am facing problem regarding to malloc...
I am allocating char * type malloc(30)....it is not giving any error or warning on allocating time but when I am going to free it it gives memory fault error..
please tell me what is wrong there and solution
Programming Language- Watcom C
OS- QNX 4.25
Regs.
Santosh |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 08, 2006 - 09:17 AM
|
|
Senior Member
Joined: Mar 18, 2005
Posts: 136
|
|
Hello,
make sure you never write more than 30 bytes to your malloc()'ed buffer - depending on the actual layout of the heap, it may be possible to write more than 30 bytes without getting an error immediately, but when you free() the buffer, the heap manager's lists are corrupted, and only then the fault occurs. Try
Code:
char* buf;
buf = (char*)malloc(30);
/* No access to buf at all */
free(buf);
and see if that is working.
Regards,
Albrecht |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 08, 2006 - 12:28 PM
|
|
QNX Master
Joined: Sep 01, 2002
Posts: 2667
|
|
You are definitely doing something. One thing is for sure you should defnitely trust that malloc is working
Post your code. |
|
|
| |
|
|
|
 |
|
|
Post subject:
Posted: Feb 23, 2006 - 12:07 AM
|
|
QNX Master
Joined: Jun 25, 2003
Posts: 976
|
|
If the code posted is literal, that is there are no instructions at all between the malloc and the free, then you have previously corrupted your allocation chain. The fact that malloc functions anyway is irrelevant.
Try this code as a free standing program and see if it works. If so, then try commenting out suspicious code previous to the malloc to see if you can make the problem go away. |
|
|
| |
|
|
|
 |
|
|
Powered by PNphpBB2 © 2003-2007 The PNphpBB Group Credits |
|
|