Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heap vs data segment vs stack allocation

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?

like image 208
Kiran Avatar asked Jun 01 '11 16:06

Kiran


People also ask

What is the difference between stack and heap allocation?

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.

Is data segment same as heap?

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.

What is difference between stack and data segment?

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.

Is stack allocation faster than heap allocation?

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 .

What is the difference between stack heap and code segment of a process?

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.

Why is stack allocation faster than heap?

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.


2 Answers

// 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 );
like image 161
Kirill V. Lyadvinsky Avatar answered Sep 20 '22 13:09

Kirill V. Lyadvinsky


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

like image 22
Šimon Tóth Avatar answered Sep 17 '22 13:09

Šimon Tóth