The Gateway to Computer Science Excellence
+1 vote
177 views
can anyone tell broadly about "Arbitrary lifetime in case of heap allocation".
in Compiler Design by (187 points) | 177 views

2 Answers

+1 vote
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.
by Loyal (5.3k points)
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.

by (339 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,645 questions
56,563 answers
195,730 comments
101,651 users