Monday, May 24, 2004

Re: Stack

"The static storage area is simply a fixed patch of memory that is allocated before the program begins to run. Using the stack or static storage area places a priority on the speed of storage allocation and release, which can be valuable in some situations."

What does "allocated before the program is run" mean exactly? Does it just mean that the OS "reserves" some area of memory and uses that as a stack (keep pushing on the function calls etc... in this area) in a FILO manner? Whereas the heap is just another chunk but you are free to allocate anywhere.


I think the answer to your queries lies in reading up on segmentation. When segmentation is used, a program which has been loaded, is allocated different segments for different parts of it.

In it's most basic sense say a program gets three segments 1) Program Instructions 2) Data 3) Call Stack. This was the basis for bring forward segmentation, so that we could get rid of the 1-D paging systems. In the 1-D system, the program, data, stack et al fall on a single piece of contiguous memory, so things could cram up when they grow. Now going back to segments, the program mentioned earlier, would have say the following segments.


| | | |
| | | | | |
| | | | | |
| | | | | |
Segemnt1 Segment2 Segment3
Program Data Call Stack


So yes, the local data in main() for a C/C++ program does get allocated when the program is loaded in memory. I am not sure as to what happens exactly happens, when say new is used. The object created, is created on the heap, but maybe it is accessed through a pointer to it's memory address, stored somewhere locally by the operating system. Just check this out, not sure how it's implemented in C++.

When the compiler compiles your C++ code, it generates native machine code right? Does the obj file only contain those instrutions?

There are three main types of object files.

1)A relocatable file holds code and data suitable for linking with other object files to create an executable or a shared object file.

2) An executable file holds a program suitable for execution; the file specifies how the execute call creates a program's process image.

3) A shared object file holds code and data suitable for linking in two contexts. First, the link editor processes the shared object file with other relocatable and shared object files to create another object file. Second, the dynamic linker combines it with an executable file and other shared objects to create a process image.

I hope that came close to answering your question, I'm not sure whether this is what you were looking for, so ask again if it was not.

I sometimes think that to be a true great when it comes to C/C++, one has to have some compiler design experience, then only you can truly know all the small nuances associated with this language.

dinesh.

No comments: