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

P

Phlip

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;).
 
V

Victor Bazarov

Phlip said:
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
 
S

Shark

Victor said:
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;


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

Pete Becker

Shark said:
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.
 
P

Phlip

Howard said:
Victor Bazarov wrote:

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.
 
P

Phlip

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

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
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top