This question already has answers here:
Is a string literal in С++ created in static memory?
(4 answers)
Where are static variables stored in C and C++?
(16 answers)
Closed 7 years ago.
How does memory gets allocated for a string literal in C and do we need to free it?
E.g.:
char *k="hello world";
Where does this string get stored and how does it get de-allocated?
where does this string get stored
Usually in read-only memory, you cannot modify it. In gcc, on most systems, they are located in the .TEXT section.
how does it get de-allocated
upon program termination.
Related
This question already has answers here:
Using pointer after free()
(6 answers)
Closed 6 years ago.
I allocated memory for pointer using malloc function. I had deallocate memory using free. But whenever I'm trying to print the content of pointer it prints the content.
Then what is the use of free()?
free() just marks the memory as available (as in, it can be used for further allocations). It does not removes the content of the memory.
That said, you should not access the content of a freed memory, as it is Undefined Behavior.
This question already has answers here:
Freeing global variable
(2 answers)
Closed 7 years ago.
Say I declare a global array int arr[10]; Should I free it somehow in the end?
You need not free it.You should use free() only when you have use malloc().
No. You can't free it and you don't need to. arr is in static storage which means that it is created on program startup and destroyed on program termination. Explicit freeing is neither possible nor required as the storage is automatically freed on program termination.
This question already has answers here:
Is a string literal in С++ created in static memory?
(4 answers)
Where are static variables stored in C and C++?
(16 answers)
Closed 7 years ago.
How does memory gets allocated for a string literal in C and do we need to free it?
E.g.:
char *k="hello world";
Where does this string get stored and how does it get de-allocated?
where does this string get stored
Usually in read-only memory, you cannot modify it. In gcc, on most systems, they are located in the .TEXT section.
how does it get de-allocated
upon program termination.
This question already has answers here:
String literals: Where do they go?
(8 answers)
Closed 9 years ago.
char *token = "some random string";
When I declare this, I know "some random string" is stored in read-only memory. My question is where will read-only memory be? Will it be in the data section or in bss?
It will be in the data section, BSS is for non-initialized memory.
If using GNU binutils, you can use nm to list the contents of an executable and see in which sections various symbols reside.
This question already has answers here:
Where are static variables stored in C and C++?
(16 answers)
Closed 9 years ago.
I know global constant will be stored in "Read Only" section. But What section of memory are static local constants stored in?
It depends on the compiler and the platform you're compiling for. They may be placed on the bottom of the stack, in the data segment of the primary thread, or simply on the heap, or even somewhere else.