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

 
 
junky_fellow@yahoo.co.in
Guest
Posts: n/a
 
      02-22-2008
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..
 
Reply With Quote
 
 
 
 
David T. Ashley
Guest
Posts: n/a
 
      02-22-2008
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

My responses below assume a "typical" compiler and development tool chain.

> 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) ?


fractionalValue will go into a data segment. The reason is that it has to
persist across function calls. (Although main() is a special case, most
compilers don't care -- they treat main() just like any other function.)

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


This will typically go on the stack. The reason is that it does not have to
persist across function calls.

> 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 ?


For storage class automatic (variables on the stack), typically the compiler
will generate code to reinitialize the variables every time the function is
entered.

Note that in your example, "fractionalValue" and "nonStatic" are initialized
in qualitatively different ways. The former normally has its data defined
so it gets loaded once as the program starts, and the latter usually has
code added by the compiler at the start of the function to assign the
variable each time the function starts. The mechanisms are normally VERY
diferent.

Most of your questions can be resolved by examining the assembly-language
output of the compiler.

Dave.

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-22-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-22-2008
Ian Collins wrote:
> (E-Mail Removed) 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. First, he needs to #define ushort. I am assuming as
"unsigned short". Then, a static variable (i.e. fractionalValue)
declared within a function is only visible within that functions,
but is kept in memory that is pre-initialized (i.e. before the
program begins to run) and which lasts until the program exits.
Without the 'static' word the object (i.e. nonStatic) is assigned
memory when the function is entered, and that memory is deassigned
when the function exits. This latter is automatic storage, and
that is all the program writer needs to know about it.

--
[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
 
santosh
Guest
Posts: n/a
 
      02-22-2008
(E-Mail Removed) wrote:

> Guys,
>
> Consider the following snippet of code:
>
> int main(VOID)


What is 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) ?


Implementation dependant. A debugger will show you where it is for a
particular run under a particular system. The C Standard only talks
about scope and linkage, nothing about placement in memory.

But for typical desktop systems fractionalValue is likely to be located
in the program's "data segment".

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


Again the C Standard does not specify any such requirement, but having
said that, under most implementations automatic objects are likely to
be on the stack.

> 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 ?


Here we come to code emission strategies which are *really* specific to
implementations and there is not much that we can say in general. The
usual method for allocations on the stack is a stack pointer decrement
followed by instructions that load the appropriate data into the newly
created space. For regular data a loop can be used. Random data will
require more instructions to generate.

For data to be placed in the data segment, it is usually "embedded" into
the executable itself and loaded by the program loader. Uninitialised
static data objects might not be represented fully in the executable
file but might be "created" during the loading process and initialised
to zeroes by the loader.

Importantly the code is likely to very different with and without
optimisations.

Since all of this is totally system specific why don't you examine the
assembler output of your implementation instead of asking here?

> thanks for any help..


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-22-2008
CBFalconer wrote:

> Ian Collins wrote:
>> (E-Mail Removed) 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. First, he needs to #define ushort. I am assuming as
> "unsigned short". Then, a static variable (i.e. fractionalValue)
> declared within a function is only visible within that functions,
> but is kept in memory that is pre-initialized (i.e. before the
> program begins to run) and which lasts until the program exits.
> Without the 'static' word the object (i.e. nonStatic) is assigned
> memory when the function is entered, and that memory is deassigned
> when the function exits. This latter is automatic storage, and
> that is all the program writer needs to know about it.


Note that the OP is asking *where* in memory both the objects would be
placed. That, AFAICS, *is* implementation dependent.

 
Reply With Quote
 
MisterE
Guest
Posts: n/a
 
      02-22-2008

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-22-2008
CBFalconer wrote:
> Ian Collins wrote:
>> (E-Mail Removed) 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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-22-2008
MisterE wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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 *******s to me.

--
Ian Collins.
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      02-22-2008
CBFalconer <(E-Mail Removed)> writes:

> Ian Collins wrote:
>> (E-Mail Removed) 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. First, he needs to #define ushort. I am assuming as


Yes it is. Did you even READ his question before piling in with your
uninformed and, as usual, incorrect opinions?
 
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