473,395 Members | 1,652 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,395 software developers and data experts.

language law - how to describe the state of uninitialized static storage

C++ dudes and dudettes:

Ogle this code:

static char * p;
struct Q { char * p; };
static Q q;

int main() {
assert(p == NULL);
assert(p.q == NULL);
}

Is its behavior well-defined? Will the assertions pass on any compliant C++
platform?

What I'm really asking is this: Uninitialized static storage is well-defined
as "all zeros". But NULL might not be all zeros. Do C++ compilers for such
platforms add extra initialization code to put NULL into unassigned static
pointers.

(BTW this is a language law question - not a style question. Write the
danged =NULL;).

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jan 19 '06 #1
7 1847
Phlip wrote:
C++ dudes and dudettes:

Ogle this code:

static char * p;
struct Q { char * p; };
static Q q;

int main() {
assert(p == NULL);
assert(p.q == NULL);
}

Is its behavior well-defined? Will the assertions pass on any
compliant C++ platform?
Yes.
What I'm really asking is this: Uninitialized static storage is
well-defined as "all zeros". But NULL might not be all zeros. Do C++
compilers for such platforms add extra initialization code to put
NULL into unassigned static pointers.
Well, it's not defined as "all zeros". It's _zero-initialised_
(see 3.6.2). For pointers it means the same as write

T * p = 0;
(BTW this is a language law question - not a style question. Write the
danged =NULL;).


V
Jan 19 '06 #2
Victor Bazarov wrote:
Phlip wrote:
C++ dudes and dudettes:

Ogle this code:

static char * p;
struct Q { char * p; };
static Q q;

int main() {
assert(p == NULL);
assert(p.q == NULL);
}

Is its behavior well-defined? Will the assertions pass on any
compliant C++ platform?


Yes.
What I'm really asking is this: Uninitialized static storage is
well-defined as "all zeros". But NULL might not be all zeros. Do C++
compilers for such platforms add extra initialization code to put
NULL into unassigned static pointers.


Well, it's not defined as "all zeros". It's _zero-initialised_
(see 3.6.2). For pointers it means the same as write

T * p = 0;
(BTW this is a language law question - not a style question. Write the
danged =NULL;).


V


The following code compiles:

#include <iostream>
using namespace std;
class A {
public:
static int &a;
};
int main()
{
return 0;
}

So is a referencing NULL?

Jan 19 '06 #3
Shark wrote:

The following code compiles:

#include <iostream>
using namespace std;
class A {
public:
static int &a;
};
int main()
{
return 0;
}

So is a referencing NULL?


No. It hasn't been defined. Try using it.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jan 19 '06 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Cf******************************@comcast.com. ..
Phlip wrote:
C++ dudes and dudettes:

Ogle this code:

static char * p;
struct Q { char * p; };
static Q q;

int main() {
assert(p == NULL);
assert(p.q == NULL);
}

Is its behavior well-defined? Will the assertions pass on any
compliant C++ platform?


Yes.


Actually, it shouldn't compile. The second assertion has p and q reversed.
It should be

assert(q.p == NULL);

-Howard
Jan 19 '06 #5
Howard wrote:
Victor Bazarov wrote:
Yes.

Actually, it shouldn't compile. The second assertion has p and q
reversed. It should be

assert(q.p == NULL);


He knew what I meant.

Thanks to all. What I was after is how to describe a compliant program's
startup situation. Code before main() is going to allocate storage for all
static and global variables.

Then it will wipe that memory with memset(storage, '\0', extent), or the
equivalent, to turn all bits off.

Then, on some platforms, it will go back and assign 0 to each pointer, where
assignment implies the pointer gets its NULL, whatever that is.

And then code before main() will call every constructor, for all the global
and static variables. The scalars that don't have a constructor will keep
their 0s, and the pointers their NULLs.

Oh, and 'static' means 'static at file scope'. Static variables inside
functions may construct as late as the first call to the function.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jan 20 '06 #6

"Phlip" <ph******@yahoo.com> wrote in message
news:Tc*****************@newssvr30.news.prodigy.co m...
Howard wrote:
Victor Bazarov wrote:

Yes.

Actually, it shouldn't compile. The second assertion has p and q
reversed. It should be

assert(q.p == NULL);


He knew what I meant.


Try telling that to my compiler! :-)

-Howard
Jan 21 '06 #7
Howard wrote:
assert(q.p == NULL);


He knew what I meant.


Try telling that to my compiler! :-)


When you are old and grizzled like me, you focus on the things your compiler
won't catch, such as complete variable names, and learn to neglect the
things your compiler will reliably complain about.

For example: Change a header file, don't change .cpp file, compile, and use
your editor's "next error" feature to navigate to each line that needs
changing. This works best if you know exactly what you can change, and what
you must inspect for yourself.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Jan 21 '06 #8

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

Similar topics

2
by: Nirvana | last post by:
What's value of uninitialized char declared outside of any function ? cheers
12
by: codefixer | last post by:
Hello: I am trying to understand the use of static in this program. http://nanocrew.net/sw/nscdec.c for "inverse" matrix. What difference would it make if it were not static and just "const...
3
by: julien | last post by:
Hello, Is it possible if a boolean was initialized or not? For other types of variable, I usually check if it is null. But this not possible for a boolean. Thank you Julien
8
by: Anthony P. Mancini | last post by:
I'm working on a proof of concept that will ultimately be deployed on a load balancer. For the sake of a preliminary demonstration I created a C# object and marked it's attributes as Public...
11
by: Deiter | last post by:
State Machines and Coroutines The other thread on goto: Lead me to want to ask... In the spirit of state machines and coroutines, This n00b to C would like to know if setjmp/longjmp are the only...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
39
by: howa | last post by:
Hello, What are the default value for initialized variable? e.g. int d; // debug give me -858993460 char c; // debug give me -52
1
by: liquidator | last post by:
I'm in the process of updating a medium-sized Fortran program. I've just finished replacing the random number generator, which is now written in C. The C source code uses variables with "static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.