473,399 Members | 3,401 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,399 software developers and data experts.

static and globals

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
Jul 22 '05 #1
3 2280
Abhishek Pandey wrote:
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
Jul 22 '05 #2
Abhishek Pandey wrote:
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

Jul 22 '05 #3
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" <Th*************************@sbcglobal.net> wrote in
message news:41**************@sbcglobal.net...
Abhishek Pandey wrote:
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

Jul 22 '05 #4

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

Similar topics

2
by: Ryan Hubbard | last post by:
Could someone provide me with some code to hold an object reference in a static variable in a function. function a(&$t){ static $b; if(is_object($b)){ print "IN - $b->logfile"; } else {...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
3
by: noleander | last post by:
Im getting a runtime error because Ive got a static object that is not properly initialized. ---------- header file xxx.hpp --------------- class SomeClass { SomeClass() { .. constructor here...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
14
by: cs1975 | last post by:
Hi Everyone, I wanted to make a global variable double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; to static global static double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; I am updating this global...
16
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.