473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

where the storage will be allocated

Guys,

Consider the following snippet of code:

int main(VOID)
{
static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}

}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?

Also, where the storage for "nonStatic" would be allocated (again
stack or data segment) ?

Also, if storage is allocated on stack for any of them, how the values
that
were initialized at compile time are obtained ? I mean to there have
to be
some space allocated in th executable during compile time, where the
initialized values should be stored.
Or the compiler generate a code, that would allocate space on the
stack and
put all the initial values on the stack ?

thanks for any help..
Feb 22 '08 #1
8 1472
ju**********@yahoo.co.in wrote:
Guys,

Consider the following snippet of code:

int main(VOID)
What's that?
{
static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}

}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?
That's a platform/compiler issue, lot a language one.

--
Ian Collins.
Feb 22 '08 #2

<ju**********@yahoo.co.inwrote in message
news:80**********************************@f47g2000 hsd.googlegroups.com...
Guys,

Consider the following snippet of code:

int main(VOID)
{
static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}

}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?

Also, where the storage for "nonStatic" would be allocated (again
stack or data segment) ?
If it was a function and not main you would be safe to say that nonstatic
goes onto the stack.
However because its main and is the 'base' function, some compilers could
put this onto the heap. THis is all implementation dependant. Even the
static one can sometimes go into heap/stack because some processors can't
read data from code space, instead part of the setup initialises ram with
those values, even though they are static, many small microprocessors would
put the static on the heap and non static on the stack.
Feb 22 '08 #3
CBFalconer wrote:
Ian Collins wrote:
>ju**********@yahoo.co.in wrote:
>> Consider the following snippet of code:

int main(VOID) {
What's that?

A silly mistake.
>> static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}
}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?
That's a platform/compiler issue, lot a language one.

No it isn't.
Oh yes it is - the OP asks where the storage is allocated, which is
platform/compiler specific.

--
Ian Collins.
Feb 22 '08 #4
MisterE wrote:
<ju**********@yahoo.co.inwrote in message
news:80**********************************@f47g2000 hsd.googlegroups.com...
>Guys,

Consider the following snippet of code:

int main(VOID)
{
static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}

}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?

Also, where the storage for "nonStatic" would be allocated (again
stack or data segment) ?

If it was a function and not main you would be safe to say that nonstatic
goes onto the stack.
However because its main and is the 'base' function,
What? That sounds like complete bollocks to me.

--
Ian Collins.
Feb 22 '08 #5
"MisterE" <vo***@sometwher.worldwrites:
<ju**********@yahoo.co.inwrote in message
news:80**********************************@f47g2000 hsd.googlegroups.com...
> Consider the following snippet of code:

int main(VOID)
{
static ushort fractionalValue[]={
0, 100, 200, 300, 400, 500, 600, 700, 800, 900,
250, 333, 666, 750, 0, 0
};

ushort nonStatic[] = {
0, 100, 200, 300, 400
}

}

I am using gcc over cygwin. I want to know where the storage for
"fractionalValue" would be allocated (stack or data segment) ?

Also, where the storage for "nonStatic" would be allocated (again
stack or data segment) ?

If it was a function and not main you would be safe to say that nonstatic
goes onto the stack.
However because its main and is the 'base' function, some compilers could
put this onto the heap. THis is all implementation dependant. Even the
static one can sometimes go into heap/stack because some processors can't
read data from code space, instead part of the setup initialises ram with
those values, even though they are static, many small microprocessors would
put the static on the heap and non static on the stack.
Um, main *is* a function. Since it can be called recursively (whether
that's a good idea is a separate question), the compiler has to store
its automatically allocated local objects in some stack-like fashion
unless it can prove that main is never called recursively in the
program. But then, it can do the same kind of thing for any other
function; if the compiler (or linker?) can prove that two or more
calls to a function are active simultaneously, it can store that
function's local automatic objects in, say, the same place where
static objects are stored.

But this kind of thing is moderately difficult to detect, and in most
implementations avoiding the stack doesn't buy you anything anyway. I
don't know of any implementations that actually play that kind of
trick.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 22 '08 #6
MisterE wrote:
>
.... snip ...
>
If it was a function and not main you would be safe to say that
nonstatic goes onto the stack. However because its main and is
the 'base' function, some compilers could put this onto the heap.
THis is all implementation dependant. Even the static one can
sometimes go into heap/stack because some processors can't read
data from code space, instead part of the setup initialises ram
with those values, even though they are static, many small
microprocessors would put the static on the heap and non static
on the stack.
Not so. main is just another function, except that its prototype
is pre-specified. It can be called recursively, etc. Its local
variables must follow all the normal rules. For example:

[1] c:\c\junk>cat junk.c
#include <stdio.h>

int main(int argc, char **argv) {

if (argc) {
main(--argc, argv);
putchar(argc + '0');
putchar('\n');
}
return argc;
}

[1] c:\c\junk>cc junk.c

[1] c:\c\junk>.\a x y z
0
1
2
3

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

--
Posted via a free Usenet account from http://www.teranews.com

Feb 22 '08 #7
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
>Um, main *is* a function. Since it can be called recursively (whether
that's a good idea is a separate question), the compiler has to store
its automatically allocated local objects in some stack-like fashion
unless it can prove that main is never called recursively in the
program. But then, it can do the same kind of thing for any other
function; if the compiler (or linker?) can prove that two or more
calls to a function are active simultaneously, it can store that
^ never
>function's local automatic objects in, say, the same place where
static objects are stored.
In older versions of Fortran, recursion was not allowed, and it was
common to allocate fixed locations for each non-parameter variable.

-- Richard
--
:wq
Feb 22 '08 #8
On 22 Feb 2008 20:36:29 GMT, ri*****@cogsci.ed.ac.uk (Richard Tobin)
wrote:
In article <87************@kvetch.smov.org>,
Keith Thompson <ks***@mib.orgwrote:
Um, main *is* a function. Since it can be called recursively (whether
that's a good idea is a separate question), the compiler has to store
its automatically allocated local objects in some stack-like fashion
unless it can prove that main is never called recursively in the
program. But then, it can do the same kind of thing for any other
function; if the compiler (or linker?) can prove that two or more
calls to a function are active simultaneously, it can store that
^ never
function's local automatic objects in, say, the same place where
static objects are stored.

In older versions of Fortran, recursion was not allowed, and it was
common to allocate fixed locations for each non-parameter variable.
To be pedantic, I'd rather say not _supported_; it wasn't required to
work, and as you note often didn't, but the implementation wasn't
required to catch it, and usually didn't, especially if indirect.

It still isn't by default -- formally you have to specify RECURSIVE.
But on most if not all modern machines, the (performance) penalty for
using stack has been eliminated or even reversed, so compilers
often(?) use it even when not formally required. Although, at least
some Fortran compilers have options to put large and/or variably-sized
arrays in heap instead, allocated and deallocated at subprogram entry
and exit, because of (primarily) OSes that have stack size limits
small relative to that for the heap and also (importantly) the sizes
of data many Fortran programmers want to handle.

COBOL at least through 1985 also doesn't support recursion.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Mar 2 '08 #9

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

Similar topics

17
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
by: Jim Showalter | last post by:
I always thought that it is safe for a function to return a pointer to static storage. And the following code does compile quietly with: gcc -pedantic -Wall -o foo foo.c #include <stdio.h> ...
3
by: Zhigang Cui | last post by:
Hi, When I saw 'global static', the first response was there was no such term. But I realized it's an idiom later. When we learn C language, when we study C standard, when we study compiler, we...
16
by: LeTubs | last post by:
Hi All I'm just checking if my assumptions are correct If I have the following line #define CLIENT_MSG_HELLO 9 I know the define will be stored in a memory location, but what will it be...
7
by: grocery_stocker | last post by:
Given the following snippet of code: (taken from http://www.gnu.org/software/libc/manual/html_node/Malloc-Examples.html) void * xmalloc (size_t size) { register void *value = malloc (size);...
13
by: S.Tobias | last post by:
I'm examining the existence of temporary objects by looking at their addresses. The trick is to create a structure that contains an array as its first member. In an expression the array rvalue...
9
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
7
by: puzzlecracker | last post by:
Dynamically allocated objects reside on a heap, local objects on a stack. What about static objects? Thanks
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.