Click here to Skip to main content
15,890,579 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Transparent checkboxes Pin
Twister33629-Jul-06 5:46
Twister33629-Jul-06 5:46 
GeneralRe: Transparent checkboxes Pin
anthonym728-Aug-06 5:55
anthonym728-Aug-06 5:55 
QuestionSending a bit stream through the serial port (VC++ 6.0) Pin
Dhananjayak0229-Jul-06 1:00
Dhananjayak0229-Jul-06 1:00 
AnswerRe: Sending a bit stream through the serial port (VC++ 6.0) Pin
Gary R. Wheeler29-Jul-06 3:04
Gary R. Wheeler29-Jul-06 3:04 
GeneralRe: Sending a bit stream through the serial port (VC++ 6.0) Pin
Dhananjayak0229-Jul-06 20:27
Dhananjayak0229-Jul-06 20:27 
Questioni can't get my class to recognize message maps Pin
abednego0028-Jul-06 22:40
abednego0028-Jul-06 22:40 
Questionwhere does variables get stored? Pin
WCup28-Jul-06 22:33
WCup28-Jul-06 22:33 
AnswerRe: where does variables get stored? [modified] Pin
Rilhas28-Jul-06 23:41
Rilhas28-Jul-06 23:41 
In a computer there is only one variable storage area, the main computer RAM. But, for the purpose of organization and performance, it is divided into several areas.

When you C/C++ program is started, memory is requested to the operating system to create the heap and the stack. Some may argue that your global variables need a third memory area, but this is not important for this discussion.

The heap is then managed by the C library if you are using C functions, like malloc or new. When you call malloc or new then a portion of the memory allocated at the start of the program is marked as used and returned to you, or, if this memory has exhausted, a new portion is requested to the operating system. If you are using Windows functions, like GlobalAlloc, then the principle is the same, in a sense that the memory you request will be marked as used and returned to you.

Both these memory areas use what we could call the heap (not the stack). They are accessed by dereferencing pointers, like in the code below:

struct S {
int x;
int y;
int z;
};

int main(void) {
S* var_s;
var_s=new S;
if (!var_s) return __LINE__;
var_s->y=20;
}

In this code, the structure S will have a total of 12 bytes (in Win32), because each integer takes 4 bytes. The new will return some relatively unpredictable address in memory, which is not that important. The only important value to check for is zero, which means the requested memory is not available (that's why the "if" statement is there).

The returned address is not important, but for this discussion let's pretend the value 1000 was returned. This value (1000) will thus be stored in var_s (below I'll tell you about where var_s resides). The line...

var_s->y=20;

... dereferences the pointer var_s, that is, uses it's value to access the memory. The var_s->y is thus translated to "memory address 1004", since address 1000 will contain the first of the 4 bytes of the "x" member, 1004 of the "y", and 1008 of the "z". So, after the translation to address 1004, the 4-byte value 20 will be stored there.

Note that even if you check what the value of var_s is, and find out it is 1000, that doesn't mean that your RAM memory chip that contains byte number 1000 is the one being used to store the data. That is because of virtual memory, which is responsible for allocating physical RAM chip addresses and assigning to logical addresses.

When your program starts, another area of memory is allocated, the stack. This memory is just like any other, but it is considered must more volatile, and besides it is also used to memorize where to go to when functions terminate. The stack is considered to be memory like "remember this for a moment while I do something else, and then I will get back to this". So, the organization is such that when you declare local variables (like var_s) a new 4 byte location is appended to the stack.

For example, when your program is started the stack could have been created at address 5000. The address to where the main function should return to is stored there, so a stack pointer will be used to store the value and advance. For example, suppose the 4 byte address value to where main should return when it finished is 7654. Then the stack pointer (which contains the value 5000) is used to place this address on the stack, and so the 4 bytes 5000, 50001, 50002, and 5003 will contain the 4 bytes of address 7654. The stack pointer is then advanced to position 5004.

When your program reaches the declaration of var_s the stack pointer is just advanced from 5004 to 5008, effectivelly allocating 4 bytes (from 5004 to 5007) for var_s. This is why var_s will contain garbage if you do not assign it a value in the declaration.

This variable, the var_s, and the main return address use normal RAM, like any other thing that should be memorized for fast access during execution, but are located in the organized structure we call stack. This fact has several consequences:

1) The address of the variable var_s is always known by using the stack pointer, which is a physical register of the processor.

2) The stack is very dense. Everything is packed together, and only the memory area after the stack pointer is free. Everything else from the start of the stack up to the stack pointer is densely occupied.

3) All the relative addresses of things stored in the stack are predictable. For example, while inside main, the return address could also be known, independently from where your program came from, by reading memory addresses 5000 to 5003 (4 bytes before the address of var_s). In reality, when main begins there is more stuff added to the stack which makes the var_s ocuppy addresses further from the main return address, but still it is possible to check all variables and function return addresses by navigating through the stack.

4) Although everything is accessible in the stack, by navigating in it, new things can be memorized only after previously memorized things. The stack can contain no holes, and that is the main diference to the heap. So, only the most recent things can be forgotten to release stack memory for new things. This means that you cannot forget the address of contents 5000 and use it for storing other things, unless you forget about var_s (at 5004) first.

(let me just point out that I am explaining the stack using the most natural logic but, in reality, the stack pointer is located first at the end of the stack area and is decremented as the stack becomes filled, i.e., the stack fills up backwards)

So, if I understood your question correctly, the answer is: things stored in the stack will occupy very predictable locations in the logical RAM address space. These things include the return address of functions, and local variables (which include parameters passed to functions, but if they are pointers then just the pointer values, not the memory to where they point to). Things allocated with malloc, new, or GlobalAlloc will occupy highly unpredictable locations in the logical RAM address space.

The logical address space has a highly unpredictable relation with physical address space (RAM chips) due to the translations performed by the virtual memory manager. This can even include transformations where the logical address space is larger than the physical space, and the portion that is missing in the RAM chips is allocated on your hard disk.

I hope this helps,
Rilhas


Questionslider control help Pin
JesGDev28-Jul-06 22:19
JesGDev28-Jul-06 22:19 
AnswerRe: slider control help Pin
Naveen28-Jul-06 23:06
Naveen28-Jul-06 23:06 
GeneralRe: slider control help Pin
JesGDev29-Jul-06 7:26
JesGDev29-Jul-06 7:26 
GeneralRe: slider control help Pin
Justin Tay29-Jul-06 11:45
Justin Tay29-Jul-06 11:45 
GeneralRe: slider control help Pin
JesGDev29-Jul-06 12:01
JesGDev29-Jul-06 12:01 
QuestionCxImage class Pin
includeh1028-Jul-06 22:19
includeh1028-Jul-06 22:19 
AnswerRe: CxImage class Pin
Jay Zhu29-Jul-06 7:28
Jay Zhu29-Jul-06 7:28 
AnswerRe: CxImage class Pin
spacecadet1029-Jul-06 16:06
spacecadet1029-Jul-06 16:06 
Questionadd api functions to pe file Pin
zon_cpp28-Jul-06 21:33
zon_cpp28-Jul-06 21:33 
QuestionHow to use CImage class in VC++ 6.0 ? Pin
Vinod Sankaranarayanan28-Jul-06 20:39
Vinod Sankaranarayanan28-Jul-06 20:39 
AnswerRe: How to use CImage class in VC++ 6.0 ? Pin
Naveen28-Jul-06 21:18
Naveen28-Jul-06 21:18 
GeneralRe: How to use CImage class in VC++ 6.0 ? Pin
Vinod Sankaranarayanan28-Jul-06 21:34
Vinod Sankaranarayanan28-Jul-06 21:34 
GeneralRe: How to use CImage class in VC++ 6.0 ? Pin
Naveen28-Jul-06 21:37
Naveen28-Jul-06 21:37 
AnswerRe: How to use CImage class in VC++ 6.0 ? Pin
Hamid_RT28-Jul-06 21:44
Hamid_RT28-Jul-06 21:44 
Questioneve dataMS ACCSEE in VC++ Pin
thelip28-Jul-06 20:21
thelip28-Jul-06 20:21 
AnswerRe: eve dataMS ACCSEE in VC++ Pin
_AnsHUMAN_ 28-Jul-06 20:54
_AnsHUMAN_ 28-Jul-06 20:54 
AnswerRe: eve dataMS ACCSEE in VC++ Pin
Hamid_RT28-Jul-06 21:50
Hamid_RT28-Jul-06 21:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.