static keyword

F

Frank-O

class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

I don' t understand why the pointer value is necessary NULL ?
 
R

Ron Natalie

Frank-O said:
class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

I don' t understand why the pointer value is necessary NULL ?
because the default initialization of a pointer is to zero intialize
it. Since this is a static sorage object, it does get default initialized.

However, your program has UNDEFINED behavior. You are not guaranteed
of anything when you dereference a null pointer.
 
K

Kai-Uwe Bux

Frank-O said:
class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

It may print anything: "a->Display()" dereferences an uninitialized pointer
variable. The program has undefined behavior.

I don' t understand why the pointer value is necessary NULL ?

There is nothing to understand: your program has undefined behavior;
anything can happen.


Best

Kai-Uwe Bux
 
D

Dennis Jones

Kai-Uwe Bux said:
It may print anything: "a->Display()" dereferences an uninitialized
pointer
variable. The program has undefined behavior.

I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer. The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless of
the value of 'a'. That is, the ability to call methods in the class is not
dependent on having a valid, non-NULL pointer. Of course, what happens if
the method attempts to make use of data members (like 'dCol' in the example)
within the class, since the 'this' pointer is uninitialized (or NULL in this
case), is completely unknown.

For example, if Display() were written thus:

const void * Display(void) const { return this; }

Then:

A *a = (A *)0x12345678;
printf("a is %p", a->Display());

....should print: "a is 12345678" without any difficulty, even though the
memory at the address assigned to 'a' is likely to contain garbage, because
it makes no use of the 'this' pointer (which is silently passed to the
function). However, the following would probably crash:

const void * Display(void) const { dCol = 6; return this; }

because it is attempting to modify memory that it very likely does not own,
which would probably cause an access violation or core dump.

I suppose it is possible that calling a method through an invalid pointer
may result in undefined behavior in some implementations, but on every
compiler I have used, class methods are always available and callable
through uninitialized and NULL pointers -- you just have no way of knowing
what will happen if you try to access memory through the 'this' pointer.

- Dennis
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Dennis said:
the pointer. The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless
of the value of 'a'. That is, the ability to call methods in the class is
not dependent on having a valid, non-NULL pointer.

Maybe this is valid for your compiler, but is not a rule of the language.
Of course, what happens if the method attempts to make use of data members
(like 'dCol' in the example) within the class, since the 'this' pointer is
uninitialized (or NULL in this case), is completely unknown.

The difference is just that the probability of crash when doing this in any
given compiler is higher that in the other case. The correctness is the
same: none.
 
D

David W

Dennis Jones said:
I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer.

As far as the language is concerned, it is.
The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless of
the value of 'a'. That is, the ability to call methods in the class is not
dependent on having a valid, non-NULL pointer.

Depends on the compiler/hardware. It may be that on a given machine the pointer
value will be placed in a register that must hold a valid address, for access by
the member function as 'this'. In any case, what the C++ standard is all that
matters.

DW
 
S

Salt_Peter

Dennis said:
I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer. The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless of
the value of 'a'. That is, the ability to call methods in the class is not
dependent on having a valid, non-NULL pointer. Of course, what happens if
the method attempts to make use of data members (like 'dCol' in the example)
within the class, since the 'this' pointer is uninitialized (or NULL in this
case), is completely unknown.

For example, if Display() were written thus:

const void * Display(void) const { return this; }

Then:

A *a = (A *)0x12345678;
printf("a is %p", a->Display());

...should print: "a is 12345678" without any difficulty, even though the
memory at the address assigned to 'a' is likely to contain garbage, because
it makes no use of the 'this' pointer (which is silently passed to the
function). However, the following would probably crash:

const void * Display(void) const { dCol = 6; return this; }

because it is attempting to modify memory that it very likely does not own,
which would probably cause an access violation or core dump.

I suppose it is possible that calling a method through an invalid pointer
may result in undefined behavior in some implementations, but on every
compiler I have used, class methods are always available and callable
through uninitialized and NULL pointers -- you just have no way of knowing
what will happen if you try to access memory through the 'this' pointer.

- Dennis

The above doesn't make sense. Display() is a *member function*, not a
global function or a free-standing function. Whether or not the member
function is const implies that a valid entity is available. The fact
that you aren't accessing a particular object's member is only an
accident here.

In such a case, noone cares if it works on some particular compiler or
platform. Or that it works on everyday except Sundays. Its undefined
behaviour because it can't be guarenteed. Whether, where and when it
works is irrelevent.

Next you'll be suggesting that a loose/unitialized/null pointer is a
valid target for a pseudo-object.
 
D

Dennis Jones

Salt_Peter said:
The above doesn't make sense. Display() is a *member function*, not a
global function or a free-standing function. Whether or not the member
function is const implies that a valid entity is available. The fact
that you aren't accessing a particular object's member is only an
accident here.

In such a case, noone cares if it works on some particular compiler or
platform. Or that it works on everyday except Sundays. Its undefined
behaviour because it can't be guarenteed. Whether, where and when it
works is irrelevent.

Next you'll be suggesting that a loose/unitialized/null pointer is a
valid target for a pseudo-object.

No, I was only saying that with the compilers I've tried, the function table
for a class exists, even if an instance on the class does not. I do not
condone the use of code in this manner -- I only suggest that it is possible
to do (at least on the compilers I've tried), and easily explains the
behavior the OP asked about.

- Dennis
 
G

Grizlyk

Must be initialized at least to NULL, else you get access to random
memory area due to "a" can be placed to uninitialized memory area
filled with random values.
I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer. The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless of
the value of 'a'.

If member Display() is virtual, it can not be called for NULL pointer,
because class of real object can not be found.
I suppose it is possible that calling a method through an invalid pointer
may result in undefined behavior in some implementations, but on every
compiler I have used, class methods are always available and callable
through uninitialized and NULL pointers -- you just have no way of knowing
what will happen if you try to access memory through the 'this' pointer.

What is the reason to call member for NULL pointer? I think any
compiler can treat the calling as error, similar to stack overflow
check and other runtime checks, and compiler do not check for NULL
pointer only for perfomance requirements.
 
G

gw7rib

Frank-O said:
class A
{
int dCol;
public :
const char * Display(void) const { return this==NULL ? "A" : "B" ; }

};

int main(void)
{

static A * a;
printf(" a is ", a->Display());

return 0;

}

it prints A

I don' t understand why the pointer value is necessary NULL ?

Incidentally, there is a pop song called "Monster" by The Automatic
which includes the line "You can't avoid static abuse". Presumably this
is what they were thinking of when they wrote it... :)
 
G

gw7rib

Dennis said:
I do not believe that is true. a->Display() is not actually "dereferencing"
the pointer. The Display() method exists regardless of whether or not any
instances of 'A' are created, and it will get invoked via a->, regardless of
the value of 'a'. That is, the ability to call methods in the class is not
dependent on having a valid, non-NULL pointer.

Suppose A contained one or more virtual functions. Wouldn't the
compiler be likely to read the object, in order to find a vtable?

Or are you relying on the compiler spotting that there are no virtual
functions and so not attempting to use the vtable mechanism?
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top