code is :
code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);
whats wrong with this?
code is :
code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);
whats wrong with this?
code is :
code is :
path = (char*)malloc(100);
pname = (char*)malloc(100);
path = "ragHu";
strcpy(pname, path);
free(path);whats wrong with this?
path = (char*)malloc(100);
statement allocates heap and assign base address to the path if allocation is success. (we assume that the allocation is succeed)
path = "ragHu";
- New heap is prepared to store "ragHu" and it base address of newly created heap is assigned.
and finally,
strcpy(pname, path);
free(path);
It's a memory leak!!!
Pramoda.M.A> whats wrong with this?
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 */
/* pname needs to be freed at some point */
Thank AIa, but I got confusion about the statement you wrote.
it is not recomened to cast malloc, just include stdlib.h and it'll know what to do ?
The problem is with the line
path = "ragHu";
It should be replaced with the following
strcpy(path, "ragHu");
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
adatapost> Thank AIa, but I got confusion about the statement you wrote.
Explanations of ... >casting malloc
adatapost> Link Explanations of ... >casting malloc has inappropriate explanation of malloc function.
originally, the prototype of malloc function is :
void *malloc(size_t size);
adatapost> Link Explanations of ... >casting malloc has inappropriate explanation of malloc function.
originally, the prototype of malloc function is :
void *malloc(size_t size);
You are in error. There was not such a thing as a void type at the inception of the C language. Read the explanation piece again, if you must.
Buddy Aia,
I have strong reason to say the return type of malloc is void. Following two line from MSDN are :
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.
> 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".
Hi Salem.
I didn't got you. What do you say about the malloc() function.
Buddy Aia,
I have strong reason to say the return type of malloc is void.
Let me try to make you understand.
The practice of casting malloc comes from way before there was a type void in the language C. char * was used as kind of generic pointer to fill the need.
Today we enjoy the void type, and malloc makes use of it. To cast malloc is by-passing the prescribed prototype defined in the stdlib.h header file; and if you do, it is at your own peril.
The current practice of casting malloc stems from people trying to write C code using a C++ compiler.
C++ does NOT allow void* to T* conversions without a cast (which is exactly what a malloc result assignment is).
Otherwise known as bashing the slightly round peg into the slightly square hole.
The solution is to commit to using either C or C++ (figure out how to compile it as C, or use 'new' in place of malloc).
To do otherwise is to have a mish-mash of random syntax called c/c++ which just about does what you want, so long as you don't do anything more complicated than the usual student assignment.
Occasionally unavoidable if you're maintaining something with history. But students writing new code should really be making better use of the tools.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.