473,408 Members | 2,832 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,408 software developers and data experts.

static keyword

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 ?

Jan 10 '07 #1
12 1627
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.

Jan 10 '07 #2
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
Jan 10 '07 #3

"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...
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
Jan 10 '07 #4
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
Jan 10 '07 #5

"Julián Albo" <JU********@terra.eswrote in message
news:45********@x-privat.org...
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
Jan 11 '07 #6
"Dennis Jones" <no****@nospam.comwrote in message
news:QKdph.18035$Pe7.5726@trnddc04...
>
"Kai-Uwe Bux" <jk********@gmx.netwrote 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
Jan 11 '07 #7

Dennis Jones wrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...
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.

Jan 11 '07 #8
"David W" <no@email.providedwrote in message
news:lj*******************@nasal.pacific.net.au...
>
In any case, what the C++ standard
says
is all that matters.
DW
Jan 11 '07 #9

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@k58g2000hse.googlegr 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
Jan 11 '07 #10
Dennis Jones wrote:
Frank-O wrote:
static A * a;
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.
printf(" a is ", a->Display());
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.

Jan 14 '07 #11

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 ?
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... :-)

Jan 14 '07 #12

Dennis Jones wrote:
"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:eo**********@murdoch.acc.Virginia.EDU...
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.
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?

Jan 14 '07 #13

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

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
6
by: The8thSense | last post by:
how to declare static varible and static method inside a class ? i try "public static ABC as integer = 10" and it said my declaration is invalid Thanks
10
by: shantanu | last post by:
Hi, How can we declare a static variable or function declared in one source file, which is to be used in other source file?
4
by: nospam_timur | last post by:
Let's say I have two files, myfile.h and myfile.c: myfile.h: int myfunction(int x); myfile.c: #include "myfile.h"
32
by: lcdgoncalves | last post by:
Hi everyone Is there a real need to use keyword static with functions, if we simply don't declare their prototypes in .h file? Many textbooks avoid to discuss this matter and/or discuss only...
2
by: DaTurk | last post by:
Hi, I have an interesting issue, well, it's not really an issue, but I'd like to understand the mechanics of what's going on. I have a file, in CLI, which has a class declared, and a static...
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...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.