803 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.8k
views
2 answers
1 votes
Prince Sindhiya asked Dec 27, 2018
1,762 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
949
views
1 answers
1 votes
Na462 asked Oct 8, 2018
949 views
Which of the Following is True ?A. Symbol table Construction is during the analysis part of the Compiler.B. Type checking is Done during Syntax Analysis phaseC. ... evaluationD. Both A and CPlease Explain the C part only rest are easy :)
1.8k
views
4 answers
2 votes
rahul sharma 5 asked Jan 24, 2018
1,832 views
Match the following with respect to activation record fields:A 1 → A, D; 2 → B, CB 1 → A, C; 2 → B, DC 1 → B, C; 2 → A, DD 1 → B, D; 2 → A, CDoubt:- Control link points to caller activation record.Can some one confirm?
3.2k
views
4 answers
2 votes
rahul sharma 5 asked Jan 24, 2018
3,163 views
Consider the following statements:S1 : Static allocation can not support recursive function.S2 : Stack allocation can support pointers but can not deallocate storage at run-time.S3 : ... ?a S1 and S2b S2 and S3c S3 and S1d S1, S2 and S3