Where in Memory is the Stack and Heap?
For a given process, a block of addresses are virtually assigned by the operating system for the stack, heap, data, and text of the code.Per our diagram here is a summary of what each memory segment does:
- Text/Code: This is total instructions for running a process and does not change during the runtime. This is where your compiled code resides in memory (traditionally, this would be the assembly code as it was written for the application). It does contain the code for the functions, but is not where memory is stored for the runtime of the function. This segment will not change in size during the runtime of the process.
- Data/Global/Static: “Data” can be a confusing term for this segment since both the stack and the heap do contain in-memory data. However, this segment is specifically reserved for data that will be maintained for the lifetime of the process. In many programing languages this is known as global or static data. It should be noted that the primitive values, structs and object pointers are stored in this segment, but the dynamic data for an object is still stored in the heap (more on this later).
- Stack: This contains a series of stack frames. Each stack frame contains addresses for a function’s return value, arguments, local variables, and back-pointers to the previous function code.
- Heap: All object values that are referenced in the static and stack memory.
Ultimately, what is the difference between the Stack and the Heap?
I believe two fundamental statements should help clarify this:- Primitive types (int, long, double, bool, etc) and structs (Guid, Size, Rectangle, etc) are stored directly on the stack.
- Object types are on the heap, but a variable’s (meaning the variable within the scope of the calling function) memory address pointing to a location on the heap is still on the stack.
public bool Foo(string a, Employee b, long c)
{
int e = 2;
Manager f = GetManager(a);
return f.YearsWithCompany + e > c - b.YearsWithCompany; // Return value
}
This is a little over simplified, since a, b, and f are stored on the stack as variable addresses that point to their corresponding values stored on the heap. However, both c and e are values stored directly on the stack. Also, it should be noted that even though YearsWithCompany is an int (primitive) value it is still stored in the heap since it is value of the Manager and Employee objects.
References:
http://www.youtube.com/watch?v=_8-ht2AKyH4
http://stackoverflow.com/questions/203695/does-using-new-on-a-struct-allocate-it-on-the-heap-or-stack
ReplyDeleteHey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing. iot course fees in chennai | iot certification courses in chennai | iot training institutes in chennai | industrial iot training chennai
Outstanding blog thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
ReplyDeleteCheck out : big data training and placement in chennai
big data hadoop training in chennai
big data certification in chennai
hadoop big data training in chennai
Great information.
ReplyDeleteIt was very Useful.
Keep sharing.
Best Java training in bengaluru
All the posts are very informative for the people who visit this site.
ReplyDeleteGood work!
tezlyrics.com
This is a very great post and the way you express your all post details that is too good. thanks for sharing-wpgroup,9xMovie,TamilMV ,Jio Rockers,movieswood,9xRockers,Full Form
ReplyDeleteGreat Post, Thank you so much for sharing this valuable post. I really like your article quality, and I really love your all articles. TamilRockers, TamilYogi, 7StarHD, ExtraMovies, PagalWorld, Filmywap, Filmyzilla, WPGroup
ReplyDeleteThis Article is Really Fantastic And Thanks For Sharing The Valuable Post.
ReplyDeleteInstant Clipping Path Services | Get Clipping Path Services In 30 Minutes.
HTML Introduction
ReplyDeleteHTML tags list with examples
HTML elements list
HTML attributes vs properties
HTML single line comment
HTML h1 to h6 Tags
HTML text formatting
HTML list-style
www.josephgiretqc.co.uk
ReplyDeletewww.berkshireclockrepair.co.uk
Forex Broker Canada Best Forex Brokers For Your Forex Trading And Fx. The Profanity Review And Forex Today Reviews The Forex Brokers And Provides The Best Fx Ratings And Reviews.
ReplyDeleteDo you believe in long term investement . One of the option of doing investement is by investing in Crypto currencies. You can invest in Fudxcoin company that deals in the selling and purchasing of Crypto Currency. It is a reliable company. One need not doubt in investing in it as i have also bought crypto currency from it and feeling very satisfied with their services.
ReplyDeletecrypto currency block chain technology
I want the world to know about where to invest their hard earned money and get fruitful returns. If one is looking forward of investing he can go into investment of crypto coins.
You can invest in Fudxcoin company that deals in the selling and purchasing of Crypto Currency. It is a reliable company. One need not doubt in investing in it as i have also bought crypto currency from it and feeling very satisfied with their services.
crypto currency block chain technology
This post is so useful and informative.Keep updating with more information.....
ReplyDeletePython Programming
Python From Basics
python training in Hyderabad
ReplyDeleteThis post more meaningfull and usefull for digital markeing course
ReplyDelete