You are here:

C++/Memory allocation


      Can you please clarify me

what are diff segments? what are the variables stored in these segments?
in which segment heap memory will be placed?
what is relation b/w Segments and RAM?

Many thanks in Advance..

C++ does not specify anything about 'segments', 'RAM', 'ROM' etc.
However, in practice, segments and segmented addressing are anachronisms introduced a long time ago; they are obsolete and no longer in use on modern hardware.

Most, if not all, modern hardware architectures and operating systems run a process in a 'virtual' address space. Pointers that you use in your program contain virtual addresses; these are translated into physical addresses by the hardware at run-time. The same virtual address may refer to different physical (RAM etc.) memory locations at different points of time. And if demand paging is used, it may not have a corresponding physical address at all at some point of time. (The page has been paged out to disk and you get a page fault).  

Functions (code) is stored in a part of memory which is separate from that of variables (also called objects or data). On certain architectures, this area is called a code segment. For a programmer, this is largely uninteresting as the area of memory containing code can not be modified in any way at run time. All you can do is execute the code for the functions which are predetermined at the time you compile and link the code - functions can not be modified and new functions can not be created at run time.

As C++ programmers, we are interested in where our variables (objects) are stored, what is their life-time and so on. We can create objects at run time and can specify what their storage duration is. Where a variable is stored and the life time of the variable is determined by the storage duration of the variable.

Variables with a static storage duration are conceptually stored in the 'static data area' - an area of memory which is pre-allocated and is available as long as the program runs.

Variables with an automatic storage duration are conceptually stored in the 'frame for the function'. The 'frame' is an area of memory which is created when a function is entered into, and is  reclaimed when the function returns. A common implementation is to create the frame for the function on the run-time call stack.

Variables with a dynamic storage duration are conceptually allocated from the 'free store' - an area of memory which from which portions can be requested at run-time as the needd arises.

In computer science, dynamic memory allocation is commonly known as 'heap-based memory allocation' - the large pool of unused memory area is called the 'heap'. C++ uses the term 'free store' instead, primarily to distinguish between new and delete (which use the free store) and C library malloc/realloc/free (which uses the C run-time heap).  


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.