path = (char*)malloc(100); /* it is not recomened to cast malloc, just include stdlib.h and it'll know what to do */
/* malloc returns NULL when it can not allocate the requested memory, checking the return is a must */
pname = (char*)malloc(100);
/* same advise than for previous statement */
path = "ragHu";
/* re-assigning path to a literal string, makes the previous malloced memory to be in limbo, you don't have any way of accessing it back or freeing it */
strcpy(pname, path);
/* what happens if there's no memory in pname to hold path? */
free(path);
/* path doesn't point to any memory that needs to be freed */
I have strong reason to say the return type of malloc is void. Following two line from MSDN are :
Quote ...
A pointer to void can be converted to or from a pointer to any type, without restriction or loss of information. If the result is converted back to the original type, the original pointer is recovered.
Last edited by adatapost; Dec 15th, 2010 at 3:07 am.
> I have strong reason to say the return type of malloc is void. Following two line from MSDN are :
The key word being "is".
Which isn't the same as "was" or "always has been".
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.