Am looking at the following program and not sure how the memory is allocated and why:
void function() {
char text1[] = "SomeText";
const char* text2 = "Some Text";
char *text = (char*) malloc(strlen("Some Text") + 1 );
}
In the above code, the last one is obviously in the heap. However, as I understand text2
is in the data segment of the program and text1
may be on the stack. Or is my assumption wrong? What is the right assumption here? Is this compiler dependent?
Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it.
The data segment contains only global or static variable which have a predefined value and can be modified. Heap contains the dynamically allocated data that is stored in a memory section we refer that as heap section and this section typically starts where data segments ends.
Uninitialized data segment or bss contains all the uninitialized global and static variables. Stack stores all local variables and arguments of functions. They also store a function return address of the instruction, which is to be executed after a function call.
Because the data is added and removed in a last-in-first-out manner, stack-based memory allocation is very simple and typically much faster than heap-based memory allocation (also known as dynamic memory allocation) e.g. C's malloc .
The operating system allocates memory for each process which is divided into segments. Stack and Heap are the two ways memory is allocated in the operating system. Stack segment is used to store local function variables that are created automatically, whereas heap segment is used for dynamically allocated memory.
The stack is faster because the access pattern makes it trivial to allocate and deallocate memory from it (a pointer/integer is simply incremented or decremented), while the heap has much more complex bookkeeping involved in an allocation or free.
// Array allocated on the stack and initialized with "SomeText" string.
// It has automatic storage duration. You shouldn't care about freeing memory.
char text1[] = "SomeText";
// Pointer to the constant string "Some Text".
// It has static storage duration. You shouldn't care about freeing memory.
// Note that it should be "a pointer to const".
// In this case you'll be protected from accidential changing of
// the constant data (changing constant object leads to UB).
const char* text2 = "Some Text";
// malloc will allocate memory on the heap.
// It has dynamic storage duration.
// You should call "free" in the end to avoid memory leak.
char *text = (char*) malloc(strlen("Some Text") + 1 );
Yes you are right, on most systems:
text1
will be a writable variable array on stack (it is required to be a writable array)
text2
has to be const char*
actually, and yes, it will point to a text segment of the executable (but that might change across executable formats)
text
will be on heap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With