Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static keyword

Reply
Thread Tools

static keyword

 
 
Frank-O
Guest
Posts: n/a
 
      01-10-2007
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 ?

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-10-2007
Frank-O wrote:
> 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.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      01-10-2007
Frank-O wrote:

> 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
 
Reply With Quote
 
Dennis Jones
Guest
Posts: n/a
 
      01-10-2007

"Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
news:eo3h2o$oam$(E-Mail Removed)...
> Frank-O wrote:
>
>> 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 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


 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      01-10-2007
Dennis Jones wrote:

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

--
Salu2
 
Reply With Quote
 
Dennis Jones
Guest
Posts: n/a
 
      01-11-2007

"Julián Albo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Dennis Jones wrote:
>
>> 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.


Granted.

- Dennis


 
Reply With Quote
 
David W
Guest
Posts: n/a
 
      01-11-2007
"Dennis Jones" <(E-Mail Removed)> wrote in message
news:QKdph.18035$Pe7.5726@trnddc04...
>
> "Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
> >
> > 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.


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


 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      01-11-2007

Dennis Jones wrote:
> "Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
> news:eo3h2o$oam$(E-Mail Removed)...
> > Frank-O wrote:
> >
> >> 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 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.

 
Reply With Quote
 
David W
Guest
Posts: n/a
 
      01-11-2007
"David W" <(E-Mail Removed)> wrote in message
news:ljfph.25753$(E-Mail Removed)...
>
> In any case, what the C++ standard


says

> is all that matters.


DW


 
Reply With Quote
 
Dennis Jones
Guest
Posts: n/a
 
      01-11-2007

"Salt_Peter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
put static keyword inside the JSP scripplets error Matt Java 3 03-15-2012 10:10 AM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 PM
Static keyword usage in asp.net? =?Utf-8?B?RGF2ZQ==?= ASP .Net 11 11-29-2005 07:11 PM
static keyword inside member function cppaddict C++ 2 05-02-2004 07:09 PM



Advertisments