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 ? 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.
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
"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
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
"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
"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
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.
"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
"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
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.
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... :-)
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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;
}
/*...
|
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...
|
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
|
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?
|
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"
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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: 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...
| |