Connecting Tech Pros Worldwide Forums | Help | Site Map

static and globals

Abhishek Pandey
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi All,
A basic question.
In C (and C++ also) global and static variables which are un-initalized are
guaranteed to be zero. This is because (as per my knowledge) they are all
stored in the uninitialized section of Data Segment (also called as BSS if I
am right) and that section is all set to NULL when the program starts.

My question is whether it is true for systems other than windows also ?
And
Where are static and global variables stored in other systems ?

Thanks
Abhishek



Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 23 '05

re: static and globals


Abhishek Pandey wrote:[color=blue]
> In C (and C++ also) global and static variables which are un-initalized are
> guaranteed to be zero. This is because (as per my knowledge) they are all
> stored in the uninitialized section of Data Segment (also called as BSS if I
> am right) and that section is all set to NULL when the program starts.
>
> My question is whether it is true for systems other than windows also ?[/color]

What's "Data Segment"? What's "BSS"? What's "windows"? The storage for
objects with static storage duration is zero-initialised at the time (and
as part) of the program starting. That's how the Standard defines it.
[color=blue]
> And
> Where are static and global variables stored in other systems ?[/color]

You would need to ask in a system-specific newsgroup.

Victor
Thomas Matthews
Guest
 
Posts: n/a
#3: Jul 23 '05

re: static and globals


Abhishek Pandey wrote:[color=blue]
> Hi All,
> A basic question.
> In C (and C++ also) global and static variables which are un-initalized are
> guaranteed to be zero. This is because (as per my knowledge) they are all
> stored in the uninitialized section of Data Segment (also called as BSS if I
> am right) and that section is all set to NULL when the program starts.[/color]
Wrong. If any guarantees are made, this is because of either the
language specification or the compiler manual. It has nothing to do
with where the variables are stored. The variables are allowed to be
stored anywhere, as long as the access and behavior matches the
statements in the standard.

There is no requirement by the C++ language specification for an
implementation to have a data segment, BSS, or code segment.


[color=blue]
> My question is whether it is true for systems other than windows also ?[/color]
Some platforms have data segments, some don't. Depends on the
implementation (compiler and linker). Some platforms may have more
segments.
[color=blue]
> And
> Where are static and global variables stored in other systems ?[/color]
There are many systems out there and I'm sure that you don't
want to know about all of them (especially the plethora of
custom embedded systems).

All you can be guaranteed of is:
1. Non-const variables are stored in places that have read and
write access.
2. Const variables (i.e. data) are may be stored in locations
that have read-only access or read and write access.

So, why do you need to know where the variables are stored?
Just initialize variables before you use them and concentrate
on making your program work correctly and robustly. After
that, then worry about size and speed.
[color=blue]
>
> Thanks
> Abhishek
>
>[/color]



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Abhishek Pandey
Guest
 
Posts: n/a
#4: Jul 23 '05

re: static and globals


Hi,
Thanks to both of the answer providers.
I forgot that I am writing to a platform independent group when I was
talking mostly about the implementation on Windows.

So now I understood that it is a requirement by the language standard itself
to have a zero initliazed static and globals which has nothing to do with
the memory storage.

Thanks
Abhishek




"Thomas Matthews" <Thomas_MatthewsSpamBotsSuck@sbcglobal.net> wrote in
message news:41E54F4D.7080604@sbcglobal.net...[color=blue]
> Abhishek Pandey wrote:[color=green]
> > Hi All,
> > A basic question.
> > In C (and C++ also) global and static variables which are un-initalized[/color][/color]
are[color=blue][color=green]
> > guaranteed to be zero. This is because (as per my knowledge) they are[/color][/color]
all[color=blue][color=green]
> > stored in the uninitialized section of Data Segment (also called as BSS[/color][/color]
if I[color=blue][color=green]
> > am right) and that section is all set to NULL when the program starts.[/color]
> Wrong. If any guarantees are made, this is because of either the
> language specification or the compiler manual. It has nothing to do
> with where the variables are stored. The variables are allowed to be
> stored anywhere, as long as the access and behavior matches the
> statements in the standard.
>
> There is no requirement by the C++ language specification for an
> implementation to have a data segment, BSS, or code segment.
>
>
>[color=green]
> > My question is whether it is true for systems other than windows also ?[/color]
> Some platforms have data segments, some don't. Depends on the
> implementation (compiler and linker). Some platforms may have more
> segments.
>[color=green]
> > And
> > Where are static and global variables stored in other systems ?[/color]
> There are many systems out there and I'm sure that you don't
> want to know about all of them (especially the plethora of
> custom embedded systems).
>
> All you can be guaranteed of is:
> 1. Non-const variables are stored in places that have read and
> write access.
> 2. Const variables (i.e. data) are may be stored in locations
> that have read-only access or read and write access.
>
> So, why do you need to know where the variables are stored?
> Just initialize variables before you use them and concentrate
> on making your program work correctly and robustly. After
> that, then worry about size and speed.
>[color=green]
> >
> > Thanks
> > Abhishek
> >
> >[/color]
>
>
>
> --
> Thomas Matthews
>
> C++ newsgroup welcome message:
> http://www.slack.net/~shiva/welcome.txt
> C++ Faq: http://www.parashift.com/c++-faq-lite
> C Faq: http://www.eskimo.com/~scs/c-faq/top.html
> alt.comp.lang.learn.c-c++ faq:
> http://www.comeaucomputing.com/learn/faq/
> Other sites:
> http://www.josuttis.com -- C++ STL Library book
> http://www.sgi.com/tech/stl -- Standard Template Library
>[/color]


Closed Thread


Similar C / C++ bytes