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.
Related
This question already has answers here:
How to find uninitialized variables in C on Linux?
(2 answers)
How to check if a variable has been initialized in C?
(3 answers)
Closed 2 years ago.
I pass a pointer to my function but if this is not initialized I get, obviously, segmentation fault. I could solve this porblem by assigning NULL to that pointer when declared (or, in general, before passing it to my function), but if I wanted my funtion to detect that pointer is not initialized?
The common way is to always initialize pointers to NULL when you first declare them or after freeing them so that whenever you have to manipulate a pointer, if it's equal to NULL, you know that you have to allocate them and populate their data.
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:
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:
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:
Why are two different concepts both called "heap"? [duplicate]
(9 answers)
What is a Memory Heap?
(8 answers)
Closed 9 years ago.
Can anyone explain why the pool of memory managed by malloc() / free() is called a heap?
Based on [1]: http://www.google.com/url?q=http://gee.cs.oswego.edu/dl/html/malloc.html&sa=D&sntz=1&usg=AFQjCNHaQLotbBKKwYqxiiYWN1146BWzFw "Doug Lea's explanation of how his malloc() works",
it's not obvious that the data structure which we call a "heap" is being used at all.
Do we call it a "heap" because it's common for malloc() implementations to use best-fit selection of the memory chunk to return, and that's historically been implemented using a min-heap of chunks, sorted by chunk size?