Aspects


vs


Malloc

The malloc function performs dynamic memory allocation in C and is part of the standard library

Strdup

The strdup() function duplicates a string

Others

Example

Note that strdup is not part of the c standard;the newemployee function return type is but you are not returning anything from this function if the malloc is successful

from question  

C Linked list - value of pointer changing

You can use either to create a new memory block which is separate from the original but naturally strdup is simpler since it doesn t require a separate malloc strlen call

from question  

Deep copying structs with char arrays in C (How to copy the arrays?)

What is the difference between case1 case2 case2 results in a crash. strdup is as good as malloc followed by strcpy

from question  

Free'ing results in crash

Or indeed if your system has strdup or you re willing to write an implementation then strdup is much cleaner than malloc + strcpy

from question  

Valgrind - snprintf : Conditional jump or move depends on uninitialised value(s)

In returning a string the example used the function strdup to allocate a string passed with a np_something method;fact is that npapi takes care of the allocated string from that point on and when tries to destroy it it cannot since strdup uses malloc and not npn_memalloc

from question  

Firefox [npapi] plugin development - firefox freeze when calling a method

You should know that strdup allocates more memory and returns its pointer which you then overwrite the original pointer returned by malloc so it will be impossible to free that memory since you no longer have the pointers

from question  

Segmentation Fault Using strcat()

The memory in strdup is created by using malloc not new;strdup - duplicate a string

from question  

Should a destructor be included with a pointer using automatic storage?

Back to Home
Data comes from Stack Exchange with CC-BY-SA-4.0