When we write the following line of code in C,
char local_arr[] = \"I am here\";
the literal \"I am here\" gets stored in the read only
C does not have garbage collection, so if you forget to deallocate allocated memory with the proper deallocator, you get a memory leak.
While sometimes a conservative garbage collector like the Boehm collector is used, that causes lots of extra headaches.
Now, there are four types of memory in C:
malloc
, calloc
, realloc
and the like return on request. Do not forget to free
resp. using another appropriate deallocator.Your example uses automatic memory for local_arr
and leaves the implementation free to initialize it to the provided literal whichever way is most efficient.
char local_arr[] = "I am here";
That can mean, inter alia:
memcpy
/strcpy
and putting the literal into static memory.Also of interest, C constant literals have no identity, so can share space.
Anyway, using the as-if rule, many times static (and even dynamic / automatic) variables can be optimized away.