Example 1:
char* var1 = "Random string";
Example 2:
char* firstline;
fscanf(input, "%s", firstline);
Example 3:
char* names[] = {"Karla", "Rob", "Tom"};
CodePudding user response:
There is no dynamically allocated memory in the examples you show.
char* var1 = "Random string";
The memory for var1
is allocated either statically (for the duration of the program) or automatically (for the duration of execution of its associated block), depending on where this declaration appears. In either case, it is managed by the C implementation, and you do not need to do anything to release it.
The memory for "Random string"
is allocated statically.
char* firstline; fscanf(input, "%s", firstline);
As above, the memory for firstline
is allocated either statically or automatically. However, firstline
must be set to point to memory before passing it to fscanf
. Your example does not show this. If firstline
is set to memory allocated dynamically, as with malloc
, then good practice is often to call free
to release the memory when it is no longer needed. (This is not necessary when exiting a program in a general-purpose multi-user operating system.)
char* names[] = {"Karla", "Rob", "Tom"};
This the same as the first example.