764 views

2 Answers

2 votes
2 votes
Malloc, calloc are primary examples of heap allocation. Once we have assigned a memory using these functions then it is upon us, the programmer, when we free them them using "free" and thus the word arbitrary. This is the reason why heap is dynamic memory allocation.
0 votes
0 votes

Lifetime cannot be arbitrary. Allocation in Data, BSS remains till the end of the program. Allocation in the heap remains until the program break is moved or that memory is reallocated. Same for the stack.

Consider the following example

int func(int n)
{
   int x = n;
   return x+x;
}

int main()
{
   int x = 5;
   x = func(x);
   long* ptr = &x;
   printf("Argument : %ld\n", *ptr);
   printf("Return Address: %p\n", ptr[-3]);
}

When compiled under gcc-7 (Ubuntu 7.3.0-16ubuntu3) 7.3.0, gives the output

Argument : 10
Return Address: 0x80484db

Where the return address represents the address at which the execution resumes after returing from func.

Related questions

1 votes
1 votes
2 answers
1
Prince Sindhiya asked Dec 27, 2018
1,594 views
The two basic operations that are often performed with the symbol table are:1.Set and reset 2.Set and insert 3. Insert and lookup 4.Reset and lookup