Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > where the storage will be allocated

Reply
Thread Tools

where the storage will be allocated

 
 
Keith Thompson
Guest
Posts: n/a
 
      02-22-2008
"MisterE" <> writes:
> <> wrote in message
> news:80e656e0-4364-4ce7-8717-...
>> 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) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      02-22-2008
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

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      02-22-2008
In article <>,
Keith Thompson <kst-> wrote:

>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
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      03-02-2008
On 22 Feb 2008 20:36:29 GMT, (Richard Tobin)
wrote:

> In article <>,
> Keith Thompson <kst-> wrote:
>
> >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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is enterprise storage so much more expensive than personal storage? John Computer Support 4 03-17-2008 09:50 PM
How to access the external storage unit of storage router =?Utf-8?B?SWduYXRpdXM=?= Wireless Networking 4 11-06-2006 06:40 AM
Difference b/w storage class and storage class specifier sarathy C Programming 2 07-17-2006 05:06 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
MCSD.net Exams allocated time IMRAN SAROIA Microsoft Certification 0 04-07-2004 03:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57