void x( )
{
strcpy(a, strdup(p) );
}
(error) Allocation with strdup, strcpy doesn't release it
Can anyone tell me what's wrong with above statement and why I am getting this error?
The problem is that you are leaking memory. The call to strdup
allocates memory which is not freed. The pointer to the memory that is passed to strcpy
is never saved anywhere and the compiler can therefore prove that it is leaked.
I'm not sure what you are trying to do since strdup
performs both allocation and copying, the call to strcpy
seems superfluous.
strdup doing something similar to this (Taken from paxdiablo's answer here) :-
char *strdup (const char *s) {
char *d = malloc (strlen (s) + 1); // Allocate memory
if (d != NULL)
strcpy (d,s); // Copy string
return d; // Return new memory
}
SO leaking the memory which has been allocated inside strdup leads to that error .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With