473,387 Members | 1,510 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,387 software developers and data experts.

Need help for NULL constant in C++

Hi all!
I have one question, want to have your help for answer it.

This is a problem:
class A
{
public:
int n;
void printMessage()
{
printf("hello world");
}
};

void main()
{
A *a = NULL;
a->printMessage();
}

I don't know why when debug: a = 0x0000000;
but we still call printMessage function.
Thank for your help!

Bui Minh Tu

Sep 11 '07 #1
8 1265
love307 wrote:
Hi all!
I have one question, want to have your help for answer it.

This is a problem:
class A
{
public:
int n;
void printMessage()
{
printf("hello world");
}
};

void main()
{
A *a = NULL;
a->printMessage();
}

I don't know why when debug: a = 0x0000000;
Because you set a = NULL. What do you expect?
but we still call printMessage function.
You are dereferencing a null pointer. Thus, you have undefined behavior,
i.e., everything (including what you observed) can happen. (In particular,
the program is not required to crash, which is why this kind of bug easily
goes uncaught. Run your program through a memory checker like valgrind to
check for mishaps like this.)

What is happening behind the scenes is possibly something like this: the
compiler knows the static type of a to be A*. The function printMessage()
is non-virtual and the compiler just deduces what code needs to be executed
from the static type.
Best

Kai-Uwe Bux
Sep 11 '07 #2
On 2007-09-11 08:09, love307 wrote:
Hi all!
I have one question, want to have your help for answer it.

This is a problem:
class A
{
public:
int n;
void printMessage()
{
printf("hello world");
}
};

void main()
{
A *a = NULL;
a->printMessage();
}

I don't know why when debug: a = 0x0000000;
but we still call printMessage function.
Thank for your help!
As Kai-Uwe Bux explained this is undefined behaviour, the "problem" with
your code is that printMessage() does not access any members of the
class, if you modify it to something like

void printMessage()
{
printf("n: %d", n);
}

you will get your expected crash. In general, a function that does no in
any way access another member of the class should probably be static.

--
Erik Wikström
Sep 11 '07 #3

As Kai-Uwe Bux said, your method is not virtual, so it's call
a->printMessage();
is realized as just like
A::printMessage(& a) ;
- so it got your NULL as first ("invisible") argument "this". As you
don't use "this" (even implicitly) in your method - NULL is not used
and u have no trouble.

Sep 11 '07 #4
ye******@front.ru wrote:
As Kai-Uwe Bux said, your method is not virtual, so it's call
> a->printMessage();
is realized as just like
A::printMessage(& a) ;
- so it got your NULL as first ("invisible") argument "this". As you
don't use "this" (even implicitly) in your method - NULL is not used
and u have no trouble.
Wrong. It's undefined behavior, so it can do anything, including
crashing, working "as expected" (whatever that is), or doing anything
else within (or out of) reason.
Sep 11 '07 #5
On 11 , 20:46, red floyd <no.s...@here.dudewrote:
year1...@front.ru wrote:
Wrong. It's undefined behavior,
Did I anywhere say this is defined behaviour?
I did explaned what happend with code of question mostly possible.
It's right way.
You gave formal point w/o explanation. Generally it's wrong way of
teaching and answering.
Kai-Uwe Bux said long before my and you:
>You are dereferencing a null pointer. Thus, you have undefined behavior
Read carefully.

Sep 11 '07 #6
ye******@front.ru wrote:
On 11 , 20:46, red floyd <no.s...@here.dudewrote:
>year1...@front.ru wrote:
Wrong. It's undefined behavior,
Did I anywhere say this is defined behaviour?
I did explaned what happend with code of question mostly possible.
It's right way.
You gave formal point w/o explanation. Generally it's wrong way of
teaching and answering.
Kai-Uwe Bux said long before my and you:
>You are dereferencing a null pointer. Thus, you have undefined behavior
Read carefully.
And you are wrong. There is no requirement for an "implied" parameter.
His implementation *may* do things that way, but how do you know that
for a fact?

Sep 11 '07 #7
On Sep 11, 11:09 am, love307 <minhtu...@gmail.comwrote:
Hi all!
I have one question, want to have your help for answer it.

This is a problem:
class A
{
public:
int n;
void printMessage()
{
printf("hello world");
}

};

void main()
{
A *a = NULL;
a->printMessage();

}

I don't know why when debug: a = 0x0000000;
but we still call printMessage function.
Thank for your help!

Bui Minh Tu


one possible reason of this may be that the memory for a varibale is
allocated at the run time but compiler , at the compile time resolves
the call for a function
and hard code it in the binary file at the build time and thats why I
believe this code is working.

Sep 12 '07 #8
On 2007-09-12 05:57:50 -0400, Hemu <he*******@gmail.comsaid:
On Sep 11, 11:09 am, love307 <minhtu...@gmail.comwrote:
>>
I don't know why when debug: a = 0x0000000;
but we still call printMessage function.
Thank for your help!

Bui Minh Tu

one possible reason of this may be that the memory for a varibale is
allocated at the run time but compiler , at the compile time resolves
the call for a function
and hard code it in the binary file at the build time and thats why I
believe this code is working.
Well, the code is "working" in the broad sense that it's doing
something reasonable that can be explained without excessive logical
contortions. Nevertheless, it isn't "working" in the narrow sense of
doing what the language definition says that it does, because the
language definition doesn't say what it should do. The code is simply
wrong, and speculating about why it does what it does is wasted time.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 12 '07 #9

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

Similar topics

11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
69
by: Ken | last post by:
Hi all. When referring to a null pointer constant in C++, is there any reason to prefer using 0 over a macro called NULL that is defined to be 0? Thanks! Ken
14
by: William Ahern | last post by:
is this legal: FILE *func(void) { extern int errno; return (errno = EINVAL, NULL); } my compiler (gcc 2.9.x on openbsd) is complaining that i'm returning a pointer from int w/o a cast....
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
29
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's...
8
by: Xiaofeng Ye | last post by:
Sombody writes: HASH_TABLE *hashed_filenames = (HASH_TABLE *)NULL; why not: HASH_TABLE *hashed_filenames = NULL; thanks!
64
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? -...
46
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
8
by: lak | last post by:
What is the difference b/w '\0' and NULL? In which case It is useful?
42
by: polas | last post by:
Afternoon all, I have some code (which I thought was OK, but now appreciate is probably not by the standard) which contains int a; ...... if (a==NULL) { ....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.