static and globals

A

Abhishek Pandey

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
 
V

Victor Bazarov

Abhishek said:
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 ?

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.
And
Where are static and global variables stored in other systems ?

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

Victor
 
T

Thomas Matthews

Abhishek said:
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.
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.


My question is whether it is true for systems other than windows also ?
Some platforms have data segments, some don't. Depends on the
implementation (compiler and linker). Some platforms may have more
segments.
And
Where are static and global variables stored in other systems ?
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.
Thanks
Abhishek



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

Abhishek Pandey

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top