473,756 Members | 3,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2665
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.x 86 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.embed ded.

Aug 9 '08 #3
On Aug 9, 11:34*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
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******** *************** ***********@i20 g2000prf.google groups.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.i nvalidwrote:
>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
implementation s 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
implementation s 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.dart mouth.edu/secref/resources/lect2.txt>
<http://www.iecc.com/linker/>
<http://en.wikipedia.or g/wiki/Executable_and_ Linkable_Format >
<http://dirac.org/linux/gdb/02a-Memory_Layout_A nd_The_Stack.ph p>

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******** *************** ***********@t1g 2000pra.googleg roups.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.c om/group/comp.lang.c/browse_thread/thread/b3242e1e28fa20a 7/93b39ea87b317fa 6>

Aug 9 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5047
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
7
37397
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
13
2123
by: Thomas Zhu | last post by:
Hello, I know the difference between the two definations. But I do not know where are they in the memory. would someone tell me ? char s={"good", "morning"}; // at stack? char *t = {"good", "morning"}; // at heap? thanks in advance.
6
1718
by: main() | last post by:
I'm a newbie. These questions arose out of my curiosity, Please pardon me if this questions sound silly. 1. Why are the automatic variables are left uninitialized by default( as i understand only global and static variables are initialized to zero) ? What prevents a complier in doing so? If there are performance issues , why are global and static variables initialized to zero ? 2. In the following code:
58
4676
by: Jorge Peixoto de Morais Neto | last post by:
I was reading the code of FFmpeg and it seems that they use malloc just too much. The problems and dangers of malloc are widely known. Malloc also has some overhead (although I don't know what is the overhead of automatic variable sized arrays, I suspect it is smaller than that of malloc), although I'm not too worried about it. I was thinking that, with C99's variable length arrays, malloc shouldn't be needed most of the time. But I'm...
7
1920
by: william | last post by:
My question is: Specific memory block where my pointer pointing to changed strangely, seemingly that no statement changed it. Here are two examples I got: ***********1***************** I was about to read from a floppy image and build a tree for all the directories and files. My question is only about a small portion where I had debugging problem, and I marked the place below at two places using "<======================"(you can try to...
4
1705
by: acw | last post by:
I am trying to understand how IE handles automatic tag variables. I know that IE will create a global variable each time it finds a tag with its name or id attribute set. If you have more than one tag with the same name or id (yes this is bad) on a page then that global variable goes from being a tag reference to being a collection of references. In most cases if you remove the duplicate tag the global var becomes a tag reference...
0
9456
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.