472,119 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

where do the automatic variables go ?

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
Aug 9 '08 #1
25 2475
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.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 9 '08 #2
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.

Aug 9 '08 #3
On Aug 9, 11:34*pm, Richard Heathfield <r...@see.sig.invalidwrote:
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.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"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.
Aug 9 '08 #4
"sidd" <si************@gmail.comwrote in message
news:4b**********************************@i20g2000 prf.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

Aug 9 '08 #5
sidd wrote:
On Aug 9, 11:34*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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/lect2.txt>
<http://www.iecc.com/linker/>
<http://en.wikipedia.org/wiki/Executable_and_Linkable_Format>
<http://dirac.org/linux/gdb/02a-Memory_Layout_And_The_Stack.php>

Aug 9 '08 #6
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.

Aug 9 '08 #7
On Aug 10, 12:53*am, Antoninus Twink <nos...@nospam.invalidwrote:
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
Aug 9 '08 #8
"sidd" <si************@gmail.comwrote in message
news:c2**********************************@t1g2000p ra.googlegroups.com...
On Aug 10, 12:53 am, Antoninus Twink <nos...@nospam.invalidwrote:
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

Aug 9 '08 #9
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/93b39ea87b317fa6>

Aug 9 '08 #10
sidd <si************@gmail.comwrites:
On Aug 9, 11:34*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>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) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 10 '08 #11
sidd <si************@gmail.comwrites:
[...]
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.
This really is the wrong place to ask. ("Antoninus Twink" knows this
perfectly well; he's a troll.)

Your question isn't about the C language, which is what this newsgroup
discusses. Your question is about the behavior of programs under
Linux. If you post to a Linux newsgroup, you'll find lots of experts
who can answer your question better than anyone here can, and others
who will gleefully correct any errors the first round of experts might
make.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 10 '08 #12
Keith Thompson <ks***@mib.orgwrites:
sidd <si************@gmail.comwrites:
>On Aug 9, 11:34Â*pm, Richard Heathfield <r...@see.sig.invalidwrote:
>>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.
Someone already did. Someone in this group who knew the subject.
>
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
No. He didn't. Someone answered him very well here.
Aug 10 '08 #13
Keith Thompson said:
sidd <si************@gmail.comwrites:
[...]
>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.

This really is the wrong place to ask. ("Antoninus Twink" knows this
perfectly well; he's a troll.)

Your question isn't about the C language, which is what this newsgroup
discusses. Your question is about the behavior of programs under
Linux. If you post to a Linux newsgroup, you'll find lots of experts
who can answer your question better than anyone here can, and others
who will gleefully correct any errors the first round of experts might
make.
In *this* group, however, such errors will not be corrected, partly because
many of us don't even read Mr Twink's articles any more, and partly
because many of us respect the topicality conventions of the group so
we're not going to start talking about executable image formats just
because someone asks.

To the OP: Given that "Antoninus Twink" has a track record of being badly
wrong on technical issues, it would be in your own best interest to get a
response from a system expert in a group full of experts in that same
system. The trouble with Twink is that, unless you already know the
subject yourself very well indeed, it isn't always easy to tell whether
he's right or wrong. And since you had to ask the question, you don't know
the subject very well indeed, right? So you have two choices - believe a
response given by someone known to be technically unreliable, or ask the
question in a group where the subject of the question is topical, so that
the experts can apply the group correction mechanism appropriately.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 10 '08 #14
On Aug 10, 12:37*pm, Richard Heathfield <r...@see.sig.invalidwrote:
Keith Thompson said:


sidd <siddharthku...@gmail.comwrites:
[...]
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.
This really is the wrong place to ask. *("Antoninus Twink" knows this
perfectly well; he's a troll.)
Your question isn't about the C language, which is what this newsgroup
discusses. *Your question is about the behavior of programs under
Linux. *If you post to a Linux newsgroup, you'll find lots of experts
who can answer your question better than anyone here can, and others
who will gleefully correct any errors the first round of experts might
make.

In *this* group, however, such errors will not be corrected, partly because
many of us don't even read Mr Twink's articles any more, and partly
because many of us respect the topicality conventions of the group so
we're not going to start talking about executable image formats just
because someone asks.

To the OP: Given that "Antoninus Twink" has a track record of being badly
wrong on technical issues, it would be in your own best interest to get a
response from a system expert in a group full of experts in that same
system. The trouble with Twink is that, unless you already know the
subject yourself very well indeed, it isn't always easy to tell whether
he's right or wrong. And since you had to ask the question, you don't know
the subject very well indeed, right? So you have two choices - believe a
response given by someone known to be technically unreliable, or ask the
question in a group where the subject of the question is topical, so that
the experts can apply the group correction mechanism appropriately.

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

- Show quoted text -
Thanks all for your suggestions and answers, but I guess I am still
not getting the explanation I am looking for so I will go ahead
and post this on one of the linux forums
Aug 10 '08 #15
On 10 Aug 2008 at 7:37, Richard Heathfield wrote:
To the OP: Given that "Antoninus Twink" has a track record of being badly
wrong on technical issues, it would be in your own best interest to get a
response from a system expert in a group full of experts in that same
system. The trouble with Twink is that, unless you already know the
subject yourself very well indeed, it isn't always easy to tell whether
he's right or wrong.
If there is a technical error in anything I post, then point it out. But
no, all you can do is sit at the sidelines slinging mud, isn't it?

To the OP: before deciding how seriously to take Mr "Heathfield"'s slurs
against me, you might like to know that he says even worse things about
the technical competence of Jacob Navia. Jacob is one of the group's
most helpful posters, and has also WRITTEN A C COMPILER for Windows. But
according to Heathfield, Jacob knows nothing about C. In that case I'm
proud to be tarred with the same brush as Jacob.

What makes Heathfield twist reality in this way? That's a question for a
psychologist, really, but it seems clear that he's an egomaniac who
wants to be in charge of this group, and force it to follow his own very
strict definition of topicality. Just look at this thread - the
executable output by a C compiler is apparently "off topic" in a C
newsgroup! It's just nonsense.

So, an egomaniac, and probably quite a big chunk of jealousy that his
arch-enemy Jacob has actually got off his fanny and done something
useful for the C community by writing and maintaining a free compiler
and IDE, whereas Heathfield for all his posturing and blustering has
done little more than tell a thousand newbies what the correct return
type of main() is. If you ever wonder what happens to schoolyard bullies
when they grow up, just look at Heathfield.

Aug 10 '08 #16
On 10 Aug 2008 at 7:41, sidd wrote:
Thanks all for your suggestions and answers, but I guess I am still
not getting the explanation I am looking for so I will go ahead
and post this on one of the linux forums
The post by Jacob Navia that I linked to provides an excellent answer to
the question that you asked. So if you didn't get the information you
wanted out of it, then you'll need to phrase your question more
precisely. (Whether you post it here, or allow the group's bullies to
push you off somewhere else).

Aug 10 '08 #17
"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...
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.
[...]

You act as if the stack will always grow downwards; why?

http://groups.google.com/group/comp....q=stack+growth

Meanwhile, the heap starts growing upwards from above the data segment.
You seem to suggest that platforms are forced to do such a thing; why?

Aug 10 '08 #18
"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...
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.
[...]

Umm. NO, because I have created a 100% conforming malloc impl for an
embedded system with no heap:

http://groups.google.com/group/comp....7314c3f55495ac

Aug 10 '08 #19
"Chris M. Thomasson" <no@spam.invalidwrote in message
news:d_*****************@newsfe01.iad...
"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...
>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.

[...]

Umm. NO, because I have created a 100% conforming malloc impl for an
embedded system with no heap:

http://groups.google.com/group/comp....7314c3f55495ac
100% stack-based malloc is possible, even in the presence of
multi-threading. If you want to discuss the algorithm I created, post over
on comp.programming.threads.

Aug 10 '08 #20
sidd wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
>Keith Thompson said:
>>sidd <siddharthku...@gmail.comwrites:

[...]

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.

This really is the wrong place to ask. ("Antoninus Twink" knows
this perfectly well; he's a troll.)

Your question isn't about the C language, which is what this
newsgroup discusses. Your question is about the behavior of
programs under Linux. If you post to a Linux newsgroup, you'll
find lots of experts who can answer your question better than
anyone here can, and others who will gleefully correct any
errors the first round of experts might make.

In *this* group, however, such errors will not be corrected,
partly because many of us don't even read Mr Twink's articles any
more, and partly because many of us respect the topicality
conventions of the group so we're not going to start talking about
executable image formats just because someone asks.

To the OP: Given that "Antoninus Twink" has a track record of
being badly wrong on technical issues, it would be in your own
best interest to get a response from a system expert in a group
full of experts in that same system. The trouble with Twink is
that, unless you already know the subject yourself very well
indeed, it isn't always easy to tell whether he's right or wrong.
And since you had to ask the question, you don't know the subject
very well indeed, right? So you have two choices - believe a
response given by someone known to be technically unreliable, or
ask the question in a group where the subject of the question is
topical, so that the experts can apply the group correction
mechanism appropriately.

Thanks all for your suggestions and answers, but I guess I am still
not getting the explanation I am looking for so I will go ahead
and post this on one of the linux forums
If you just bothered to read the responses you did get, and quoted
above, you will find out why this is the wrong place to ask. And
you want more than just a linux group, you want one that deals with
your actual machine. Linux runs on many widely differing
computers.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Aug 10 '08 #21
In article <H%*****************@newsfe01.iad>,
Chris M. Thomasson <no@spam.invalidwrote:
....
>100% stack-based malloc is possible, even in the presence of
Um, er, you just used the "S-word" in public. Please go and wash your
mouth out with soap.

I expect Mr. Keith to come along any moment now (almost certainly within
the hour) to state that C has no stack. So, therefore, you can't
possibly have written something in C that uses a stack.

It must have been in some other, irrelevant, "C-like" language.
Maybe AWK? Maybe Perl?

Aug 10 '08 #22
"Chris M. Thomasson" wrote:
"Antoninus Twink" <no****@nospam.invalidwrote in message
>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.

[...]

Umm. NO, because I have created a 100% conforming malloc impl for
an embedded system with no heap:

http://groups.google.com/group/comp....7314c3f55495ac
Twink is a pure troll. Please don't encourage him/her/it.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Aug 10 '08 #23
On 10 Aug 2008 at 12:15, Chris M. Thomasson wrote:
You act as if the stack will always grow downwards; why?
>Meanwhile, the heap starts growing upwards from above the data segment.

You seem to suggest that platforms are forced to do such a thing; why?
I suggest no such thing. I simply point out that that is what happens on
Linux/x86, which is what the OP asked about.

Aug 10 '08 #24
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.
<OT>
Automatic variables do not exist in the executable file, in the sense
that you seem to be meaning. They are created on the stack at runtime
as needed, assuming the compiler doesn't put them into registers or
completely optimize them away. Globals and statics can be put in a
fixed part of the executable, since exactly one copy of them will exist
for the entire duration of the program, but that is not true of
automatic variables. Think about recursion in particular; each instance
of the function needs its own copy of any automatic variables.

How a stack frame is created will vary depending on the particular
processor and perhaps OS that you are using. The best thing you can do
is direct your compiler to produce unoptimized assembly code instead of
an executable (e.g. gcc -S -O0) and try to understand the output. On
every system I'm aware of, each function will have a standard "prologue"
at the beginning and "epilogue" at the end. These are what set up and
destroy the stack frame, among other things. If the compiler needs
space on the stack for automatic variables, you will see a small
variation in those two sections to account for the extra space needed.
</OT>

S
Aug 10 '08 #25
Antoninus Twink wrote, On 10/08/08 14:29:
On 10 Aug 2008 at 12:15, Chris M. Thomasson wrote:
>You act as if the stack will always grow downwards; why?
>>Meanwhile, the heap starts growing upwards from above the data segment.
You seem to suggest that platforms are forced to do such a thing; why?

I suggest no such thing. I simply point out that that is what happens on
Linux/x86, which is what the OP asked about.
The OP asked about Linux, not necessarily Linux on an x86, and I have a
desktop Linux box where the stack grows up.
--
Flash Gordon
Aug 10 '08 #26

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Jonas Rundberg | last post: by
7 posts views Thread by S. A. Hussain | last post: by
13 posts views Thread by Thomas Zhu | last post: by
58 posts views Thread by Jorge Peixoto de Morais Neto | last post: by
4 posts views Thread by acw | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.