473,246 Members | 1,476 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,246 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 1264
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: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.