[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

where do the automatic variables go ?

sidd

8/9/2008 6:27:00 PM

In the following code:

int i = 5; ---> it goes to .data segment
int j; ---> it goes to bss segment


int main()
{
int c;
int i = 5; ---> stack
int j[5] = new int[5]; ----> heap
c = i*2; ----> goes to .text segment



}


My question is : When the object file is created there are text, data
and bss segments etc...but there is notthing like stack and heap
segment, what happens to these automatic variables ?

I hope I am making sense.....


Siddharth
25 Answers

Richard Heathfield

8/9/2008 6:35:00 PM

0

sidd said:

> In the following code:
>
> int i = 5; ---> it goes to .data segment
> int j; ---> it goes to bss segment

The C Standard doesn't guarantee this. Nor does it even require that
implementations recognise the concept of .data or bss segments.

> int main()
> {
> int c;
> int i = 5; ---> stack

The C Standard doesn't guarantee this. Nor does it even require that
implementations recognise the concept of a (hardware) stack.

> int j[5] = new int[5]; ----> heap

In C, this is just a syntax error.

If you have questions about the C++ language, ask in comp.lang.c++.
If you have questions about the storage techniques used by your
implementation, ask in a group devoted to that implementation.

<snip>

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

santosh

8/9/2008 6:46:00 PM

0

sidd wrote:

> In the following code:
>
> int i = 5; ---> it goes to .data segment

Not necessarily.

> int j; ---> it goes to bss segment

Not necessarily.

> int main()
> {
> int c;
> int i = 5; ---> stack

Not necessarily.

> int j[5] = new int[5]; ----> heap

This is a syntax error in C. If your doing C++ the note that there is a
specialised group for it: comp.lang.c++.

> c = i*2; ----> goes to .text segment

Not necessarily.

> }
>
>
> My question is : When the object file is created there are text, data
> and bss segments etc...but there is notthing like stack and heap
> segment, what happens to these automatic variables ?
>
> I hope I am making sense.....

Firstly, as far as Standard C is concerned, there need be no segments of
any kind at all. In fact, the format of the final executable image
produced by the last translation phase is not specified at all. In
fact, an interpreter that never produces a machine language
representation is also perfectly legal.

The segments you have mentioned are a common system of laying out
executable files, but this is not universal, and even within the
overall scheme, there is a lot of variation in detail.

In general though, automatic objects are placed in some kind of "stack
like" memory (which under most implementations happens to be an actual
hardware assisted stack), while static objects reside elsewhere. The
compiler may also decide to place string literals and const qualified
objects in read-only storage, while memory returned by
malloc/calloc/realloc is usually from the so-called "heap", though
again it must be emphasised that C itself does not require such
classification, though it is commonly used.

For example a C implementation that decides to place all objects on the
heap is perfectly conforming, while one that decides to allocate all
objects on a stack is also perfectly conforming.

To get a more satisfactory answer (though it is not a generic one), you
must examine the details of your implementation, the memory model used
by your operating system, the format of the executable that your
implementation produces etc. An assembly language group might be a
better idea since these sorts of machine level details are often dealt
with there. See comp.lang.asm.x86 if your system is an x86 based one,
or alt.lang.asm for other architectures. If your system is an embedded
one see comp.arch.embedded.

sidd

8/9/2008 6:46:00 PM

0

On Aug 9, 11:34 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
> sidd said:
>
> > In the following code:
>
> > int i = 5;  ---> it goes to .data segment
> > int j;        ---> it goes to bss segment
>
> The C Standard doesn't guarantee this. Nor does it even require that
> implementations recognise the concept of .data or bss segments.
>
> > int main()
> > {
> > int c;
> > int i = 5; ---> stack
>
> The C Standard doesn't guarantee this. Nor does it even require that
> implementations recognise the concept of a (hardware) stack.
>
> > int j[5] = new int[5]; ----> heap
>
> In C, this is just a syntax error.
>
> If you have questions about the C++ language, ask in comp.lang.c++.
> If you have questions about the storage techniques used by your
> implementation, ask in a group devoted to that implementation.
>
> <snip>
>
> --
> Richard Heathfield <http://www.cpax....
> Email: -http://www. +rjh@
> Google users: <http://www.cpax.org.uk/prg/writings/goog...
> "Usenet is a strange place" - dmr 29 July 1999

Can someone please answer the question assuming that it was run on a
linux m/c and the executable was a.out.

Bartc

8/9/2008 6:53:00 PM

0

"sidd" <siddharthkumra@gmail.com> wrote in message
news:4b804b88-d524-4a28-8519-5eb548d51b8c@i20g2000prf.googlegroups.com...
> In the following code:
>
> int i = 5; ---> it goes to .data segment
> int j; ---> it goes to bss segment
>
>
> int main()
> {
> int c;
> int i = 5; ---> stack
> int j[5] = new int[5]; ----> heap
> c = i*2; ----> goes to .text segment
>
>
>
> }
>
>
> My question is : When the object file is created there are text, data
> and bss segments etc...but there is notthing like stack and heap
> segment, what happens to these automatic variables ?

The stack and heap areas are not needed in the executable. They are setup at
runtime. That's if they will exist at all.

--
bartc

santosh

8/9/2008 6:55:00 PM

0

sidd wrote:

> On Aug 9, 11:34 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
>> sidd said:
>>
>> > In the following code:
>>
>> > int i = 5;  ---> it goes to .data segment
>> > int j;        ---> it goes to bss segment
>>
>> The C Standard doesn't guarantee this. Nor does it even require that
>> implementations recognise the concept of .data or bss segments.
>>
>> > int main()
>> > {
>> > int c;
>> > int i = 5; ---> stack
>>
>> The C Standard doesn't guarantee this. Nor does it even require that
>> implementations recognise the concept of a (hardware) stack.
>>
>> > int j[5] = new int[5]; ----> heap
>>
>> In C, this is just a syntax error.
>>
>> If you have questions about the C++ language, ask in comp.lang.c++.
>> If you have questions about the storage techniques used by your
>> implementation, ask in a group devoted to that implementation.
>>
>> <snip>

> Can someone please answer the question assuming that it was run on a
> linux m/c and the executable was a.out.

<http://althing.cs.dartmouth.edu/secref/resources/lec...
<http://www.iecc.com/l...
<http://en.wikipedia.org/wiki/Executable_and_Linkable_...
<http://dirac.org/linux/gdb/02a-Memory_Layout_And_The_Sta...

John B. Matthews

8/9/2008 7:53:00 PM

0

On 9 Aug 2008 at 18:26, sidd wrote:
> int main()
> {
> int c;
> int i = 5; ---> stack
> int j[5] = new int[5]; ----> heap
> c = i*2; ----> goes to .text segment
> }

This is a misleading dichotomy. The new line will almost certainly
generate instructions too, which will be placed in the .text segment:
new needs to call malloc() to get its memory, which will indeed be taken
from the heap.

> My question is : When the object file is created there are text, data
> and bss segments etc...but there is notthing like stack and heap
> segment, what happens to these automatic variables ?

The kernel provides each process with a virtual address space, and takes
responsibility for mapping this to physical memory. When your C program
has been loaded and starts executing, the operating system
(executable-loader) has kindly set up this address space to look like
this:


highest address
=========
| |
| |
| |
| |
| |
| |
=========
| data |
| +bss |
=========
| text |
=========
address 0

As you program starts generating stack frames and automatic variables,
the stack grows downwards from high to low memory addresses. Meanwhile,
the heap starts growing upwards from above the data segment.

highest address
=========
| stack |
| vv |
| |
| |
| ^^ |
| heap |
=========
| data |
| +bss |
=========
| text |
=========
address 0

Things get more interesting when you add libraries, threads etc. into
the mix, but this is the basic setup.

sidd

8/9/2008 8:14:00 PM

0

On Aug 10, 12:53 am, Antoninus Twink <nos...@nospam.invalid> wrote:
> On  9 Aug 2008 at 18:26, sidd wrote:
>
> > int main()
> > {
> > int c;
> > int i = 5; ---> stack
> > int j[5] = new int[5]; ----> heap
> > c = i*2; ----> goes to .text segment
> > }
>
> This is a misleading dichotomy. The new line will almost certainly
> generate instructions too, which will be placed in the .text segment:
> new needs to call malloc() to get its memory, which will indeed be taken
> from the heap.
>
> > My question is : When the object file is created there are text, data
> > and bss segments etc...but there is notthing like stack and heap
> > segment,  what happens to these automatic variables ?
>
> The kernel provides each process with a virtual address space, and takes
> responsibility for mapping this to physical memory. When your C program
> has been loaded and starts executing, the operating system
> (executable-loader) has kindly set up this address space to look like
> this:
>
> highest address
>   =========
>   |       |
>   |       |
>   |       |
>   |       |
>   |       |
>   |       |
>   =========
>   | data  |
>   | +bss  |
>   =========
>   | text  |
>   =========
> address 0
>
> As you program starts generating stack frames and automatic variables,
> the stack grows downwards from high to low memory addresses. Meanwhile,
> the heap starts growing upwards from above the data segment.
>
> highest address
>   =========
>   | stack |
>   |  vv   |
>   |       |
>   |       |
>   |  ^^   |
>   | heap  |
>   =========
>   | data  |
>   | +bss  |
>   =========
>   | text  |
>   =========
> address 0
>
> Things get more interesting when you add libraries, threads etc. into
> the mix, but this is the basic setup.

Thanks for the wonderful links and explanations, but I guess my
question still remains unanswered. My question was that when the code
is compiled and converted to a.out, what happens to the automatic
variables, in other words can someone elaborate more about the
generation of stack frames. I understand that whenever a function is
called, a stack frame gets generated but how are the details of that
routine laid out in the a.out file. In other words where do the
automatic variables part of the routine lie in the a.out format.


Thanks,
Sidd

Bartc

8/9/2008 8:40:00 PM

0

"sidd" <siddharthkumra@gmail.com> wrote in message
news:c270d070-18d9-4b3e-812a-534c6931bec8@t1g2000pra.googlegroups.com...
On Aug 10, 12:53 am, Antoninus Twink <nos...@nospam.invalid> wrote:
> On 9 Aug 2008 at 18:26, sidd wrote:
>
> > int main()
> > {
> > int c;
> > int i = 5; ---> stack
> > int j[5] = new int[5]; ----> heap
> > c = i*2; ----> goes to .text segment
> > }

>called, a stack frame gets generated but how are the details of that
>routine laid out in the a.out file. In other words where do the
>automatic variables part of the routine lie in the a.out format.

If you get your compiler to display intermediate asm code, you will see how
automatic variables are handled.

For example, in the following function:

void foo(void) {
int a,b,c;
a=b+c;
}

the auto variables a,b,c are handled as offsets -4, -8, -12 to some stack
space obtained at runtime:

foo:
push ebp
mov ebp,esp
sub esp,12 ;allocate stack frame of 12 bytes

mov eax,[ebp-8] ;<b>
add eax,[ebp-12] ;<c>
mov [ebp-4],eax ;<a>

add esp,12 ;free the 12 bytes
pop ebp
retn

The details will of course vary greatly between machines and compilers.

--
Bartc

John B. Matthews

8/9/2008 9:09:00 PM

0

On 9 Aug 2008 at 20:13, sidd wrote:
> Thanks for the wonderful links and explanations, but I guess my
> question still remains unanswered. My question was that when the code
> is compiled and converted to a.out, what happens to the automatic
> variables, in other words can someone elaborate more about the
> generation of stack frames. I understand that whenever a function is
> called, a stack frame gets generated but how are the details of that
> routine laid out in the a.out file. In other words where do the
> automatic variables part of the routine lie in the a.out format.

Firstly, it's perhaps worth clarifying that although most C compilers
produce a file called a.out if you don't specify an output filename,
this is purely for historical reasons, and this executable need not be
in a.out format. You mentioned Linux in another post... unless you're
using a kernel that's older than version 1.2 or so, the native format
will be ELF.

As to the question about stack frames, this was explain very clearly by
Jacob Navia, one of the real C experts in this group and the creator of
a C99 compiler for Windows, a few months ago: check out his post and ask
if you have any questions.

<http://groups.google.com/group/comp.lang.c/browse_thread/thread/b3242e1e28fa20a7/93b39ea87b...

Keith Thompson

8/10/2008 3:33:00 AM

0

sidd <siddharthkumra@gmail.com> writes:
> On Aug 9, 11:34 pm, Richard Heathfield <r...@see.sig.invalid> wrote:
>> sidd said:
>> > In the following code:
>>
>> > int i = 5;  ---> it goes to .data segment
>> > int j;        ---> it goes to bss segment
>>
>> The C Standard doesn't guarantee this. Nor does it even require that
>> implementations recognise the concept of .data or bss segments.
>>
>> > int main()
>> > {
>> > int c;
>> > int i = 5; ---> stack
>>
>> The C Standard doesn't guarantee this. Nor does it even require that
>> implementations recognise the concept of a (hardware) stack.
>>
>> > int j[5] = new int[5]; ----> heap
>>
>> In C, this is just a syntax error.
>>
>> If you have questions about the C++ language, ask in comp.lang.c++.
>> If you have questions about the storage techniques used by your
>> implementation, ask in a group devoted to that implementation.
>>
>> <snip>
>
> Can someone please answer the question assuming that it was run on a
> linux m/c and the executable was a.out.

I'm sure someone can.

Richard gave you a complete and correct answer with regard to C.
If you want a system-specific answer, you'll have to ask in a
system-specific newsgroup, probably one of the comp.os.linux.* groups

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"