C++ 简单的指针初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1367363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
simple Pointer initialization
提问by paxdiablo
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.
感谢您到目前为止的所有回答。有趣的是,如果我按如下方式初始化指针,则 mem::copy 操作可以正常工作。
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...
然后我在 mem::copy 期间崩溃了...
Weird!
奇怪的!
回答by paxdiablo
Simple questions are fine, I think it's well established that SO is meant to be for all levels, not just an elite.
简单的问题很好,我认为 SO 是针对所有级别的,而不仅仅是精英,这是众所周知的。
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:
null
C 中没有(除非你自己定义它)。初始化为空指针可以通过以下两种方式之一完成:
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).
如果您p
在那之后取消引用,您可能会遇到访问冲突(我相信根据标准,这是未定义的行为,所以实际上,任何事情都可能发生,甚至包括宇宙的彻底毁灭——它甚至可能继续运行很好,但我不会依赖它)。
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:
重新编辑,这并不奇怪,您只需要对其进行可视化即可。让我们假设所有变量都是从内存位置 100 开始创建的,整数和指针的长度都是 4 个字节。将您的两种情况分解为最简单的形式:
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
.
试图改变指向的变量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).
在左侧,您将尝试将值 7 写入内存位置 0。这是一件坏事;很少有系统可以让您在不崩溃的情况下执行此操作,甚至更少的系统可以完全没有任何不利影响(某些版本的 HP-UX 实际上可以正常工作)。
On the right side is what shouldhappen. The value picked up from px
is 100, so the value 7 is written to that memory location, changing x
as intended.
右边是应该发生的事情。从中拾取的值px
是 100,因此值 7 被写入该内存位置,x
并按预期更改。
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.
我经常发现图片(即使是原始的 ASCII 艺术图片,因为我不是鲁本斯或波提切利)有助于阐明概念。希望它为您清除了一点。
回答by pierrotlefou
Reply to your question in EDIT:
在编辑中回复您的问题:
When you do this :
int tmp = 0; int *tmpPtr = &tmp; Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));
当你这样做时:
int tmp = 0; int *tmpPtr = &tmp; Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));
tmpPtr is pointing to the address of variable tmp
and it is in the stack. And notice that the "safe area" pointed by tmpPtr is the size of tmp
(which is 4 in some machine and 2 in others). If you were to copy more than sizeof(int) bytes to tmpPtr you will risk of crashing the stack.
tmpPtr 指向变量的地址,tmp
它在堆栈中。并注意 tmpPtr 指向的“安全区域”的大小tmp
(在某些机器中为 4,在其他机器中为 2)。如果您将超过 sizeof(int) 个字节复制到 tmpPtr,您将面临堆栈崩溃的风险。
When you do this :
int *tmpPtr = 0; Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));
当你这样做时:
int *tmpPtr = 0; Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));
The value of tmpPtr is 0 ,so you will get a segment fault.,which is a memory protection mechanism offered by the operation system. For example , you are not allowed to write any virtual addressthat is less than 4K.
tmpPtr 的值为 0 ,所以你会得到一个段错误。,这是操作系统提供的一种内存保护机制。例如,不允许写入任何小于 4K 的虚拟地址。
回答by Vik
Initialize your pointers which point to nothing by a null pointer constant. Any constant expressions with value 0 serve as a null pointer constant. In C NULLmacro is used by convention.
用空指针常量初始化指向空的指针。任何值为 0 的常量表达式都用作空指针常量。在 C 中,按照惯例使用NULL宏。
回答by Andrew
I was just refreshing my pointer knowledge and stumbled across this.
我只是在刷新我的指针知识并偶然发现了这一点。
int *tmpPtr = 0;
i find it easier to think of it like this:
我发现这样想更容易:
int *tmpPtr ;
tmpPtr = 0 ;
I 'believe' the above 2 lines are equivalent to that one line.
so basically the address to be de-referenced is set to 0 or NULL
.
我“相信”以上两行相当于那一行。所以基本上要取消引用的地址设置为 0 或NULL
.
回答by RC.
You can do it any of those ways except it is NULL not null. I prefer int *p = 0; over int *p = NULL;
您可以通过任何方式执行此操作,除非它是 NULL 而非 null。我更喜欢 int *p = 0; 在 int *p = NULL;
To eliminate some confusion I sense, setting int *p = 0 or setting MyObject *p = 0; results in the same thing.... a null pointer that will usually crash your program if you attempt to dereference it though technically it's undefined behavior. The example you have in C is different from the others because you are actually setting the pointer to point at an integer set as 0.
为了消除我感觉到的一些混乱,设置 int *p = 0 或设置 MyObject *p = 0; 导致同样的事情......一个空指针,如果您尝试取消引用它,通常会使您的程序崩溃,尽管从技术上讲它是未定义的行为。您在 C 中的示例与其他示例不同,因为您实际上将指针设置为指向设置为 0 的整数。
int *pNull = 0; int c = *pNull; // Undefined behavior. Most likely crashing your program! int a = 0; int *pInt = &a; int x = *pInt; // x equals 0 a = 10; int y = *pInt; // y equals 10
回答by Kirill V. Lyadvinsky
There is no null
keyword in C (at least in ANSI C99). You could use a) or c).
null
C 中没有关键字(至少在 ANSI C99 中)。您可以使用 a) 或 c)。
In c) you'll not initialize pointer with null, you'll initialize it with address of local variable.
在 c) 中,您不会用 null 初始化指针,而是用局部变量的地址初始化它。
回答by John Bode
The line
线
int *tmpPtr = 0;
initializes the pointervalue to 0, so tmpPtr is pointing "nowhere"; i.e., not a valid memory location. You have to assign a valid memory location to the pointer first like you did in the previous snippet.
将指针值初始化为 0,因此 tmpPtr 指向“无处”;即,不是有效的内存位置。您必须首先像在上一个代码段中所做的那样,为指针分配一个有效的内存位置。