Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

c++ - simple Pointer initialization

It has been a while that I used pointers and I just wanna quickly check how I can initialize an integer pointer?

a) int *tmpPtr = 0;

b) int *tmpPtr = null;

c) int a = 0;
   int *tmpPtr = &a;

EDIT

Thanks for all your answers so far. The funny thing is, that if I intitalize the pointer as follows, then the mem::copy operation works fine.

int tmp = 0;
int *tmpPtr = &tmp;
Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));

However, if I do it like this:

int *tmpPtr = 0;
Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));

then I get a crash during mem::copy...

Weird!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Simple questions are fine, I think it's well established that SO is meant to be for all levels, not just an elite.

There is no null in C (unless you define it yourself). Initializing to a null pointer can be done in one of the following two ways:

int *p = 0;
int *p = NULL;

If you dereference p after that, you're likely to get an access violation (I believe it's undefined behavior according to the standard, so really, anything could happen, up to and including, total annihilation of the universe - it may even continue to run fine, but I wouldn't rely on it).

To get a pointer to a real integer, just use:

int a = 7;
int *p = &a;

using the address-of operator.

Re your edit, it's not weird at all, you just need to visualize it. Let's pretend that all variables are created starting at memory location 100 and integers and pointers are both 4 bytes in length. Breaking your two situations down to their simplest form:

                                  int x = 2;
int *px = 0;                      int *px = &x;

         +-----+                          +-----+
px(100): |   0 |                  x(100)  |   2 |
         +-----+                          +-----+
                                  px(104) | 100 |
                                          +-----+

Then you execute the command

*px = 7;

in an attempt to change the variable pointed to by px.

On the left-hand side, you will try to write the value 7 to memory location 0. That's a bad thing; very few systems will allow you to do that without crashing, even fewer will allow it without any adverse effects at all (some versions of HP-UX actually worked okay).

On the right side is what should happen. The value picked up from px is 100, so the value 7 is written to that memory location, changing x as intended.

I often find pictures (even primitive ASCII art ones, since I'm no Rubens or Botticelli) help clarify the concepts. Hopefully it's cleared it up a little for you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...