Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if a variable is allocated in stack or heap?

Tags:

Stumbled upon this interview question somewhere,

In C, Given a variable x, how do you find out if the space for that variable is allocated on the stack or heap?

(Is there any way to find it out programatically and not having to go through the symbol table, etc? And does finding if the space is allocated in stack or heap has any practical implications?)

like image 525
Raj Avatar asked Dec 05 '12 09:12

Raj


1 Answers

No, not in general.

Do you know of gcc -fsplit-stack ?

It is up to the implementation to decide whether to allocate a contiguous stack or a stack where blocks are interleaved with heap blocks in memory. Good luck figuring out whether a block was allocated for the heap or the stack when the latter is split.

like image 113
Matthieu M. Avatar answered Oct 11 '22 14:10

Matthieu M.