Malloc Problem

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

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

char* buf; buf = (char*)malloc(30); /* No access to buf at all */ free(buf);

and see if that is working.

Regards,
Albrecht

You are definitely doing something. One thing is for sure you should defnitely trust that malloc is working ;)

Post your code.

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.