代码中未初始化的指针
我正在学习 C++,我发现指针如果未初始化可能会指向内存中的随机位置,并产生内存可能被其他程序使用的问题。
现在,如果是这种情况,我们就不应该在代码的任何部分包含这一行:
int* ptr;
相反,我们应该有类似
int* ptr = NULL; //Is this going to avoid the problem
Please suggest 因为我已经看到第一行(int* ptr;
< /strong>)在很多书中都有,所以我有这个疑问。如果可能的话还举一些例子。
I am learning C++ and I came to know that pointers if left uninitialized could point to random locations in memory and create problems that memory might be used by some other program.
Now if that is the case we should never have this line in any part of our code:
int* ptr;
Instead we should have something like
int* ptr = NULL; //Is this going to avoid the problem
Please suggest because I have seen the first line(int* ptr;
) in many books so I am getting this doubt. If possible give some examples also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这将导致
ptr
指向NULL
,您可以将其显式检查为默认/未初始化值。它可以防止您描述的问题,但粗心的程序员仍然可能在不检查的情况下意外取消引用空指针,从而导致未定义的行为。主要优点是可以方便地检查
ptr
是否已初始化为任何内容,即:由于这是非常惯用的,因此不将指针初始化为
NULL
非常危险代码>.该指针将被初始化为一个非 NULL 垃圾值,该值并不真正指向任何真实的东西。最糟糕的是,上面的检查会通过,如果碰巧指针中的地址是您可以合法访问的内存,则会导致更严重的问题。在某些嵌入式环境中,您可能能够访问内存的任何部分,因此您可能会意外损坏内存的随机部分或执行代码的随机部分。This will cause
ptr
to point toNULL
which you can explicitly check for as a default/uninitialized value. It prevents the problem you describe, but a careless programmer can still accidentally dereference a null pointer without checking, causing undefined behaviour.The main advantage is your convenience for checking whether the
ptr
has or has not been initialized to anything, ie:Since this is pretty idiomatic, its pretty dangerous to not initialize the pointer to
NULL
. The pointer would be initialized to a non-NULL garbage value that doesn't really point to anything real. Worst of all, the check above would pass, causing even worse problems if it just so happens that the address in the pointer is memory you can legally access. In some Embedded environments, you might be able to access any part of memory, so you might accidentally corrupt random parts of memory or random parts of your executing code.始终初始化变量。
有时,您可能希望初始化为
NULL
,但大多数时候,您应该能够将指针初始化为它应该保存的值。尽可能晚地声明变量,并在此时初始化它们,而不是在代码中再往下 15 行。Always initialize your variables.
Occasionally, you may want to initialize to
NULL
, but most of the time, you should be able to initialize the pointer to the value it is supposed to hold. Declare variables as late as possible, and initialize them at that point, not 15 lines further down in your code.该行:
绝对不能保证将指针值初始化为任何特定值。
该行:
将初始化指针以指向地址零,该地址实际上永远不会保存任何有用的内容,并且通常将其检查为无效指针值。
当然,正如 Doug T. 所说,尝试使用这个指针而不检查它仍然是可能的,因此它仍然会崩溃。
显式初始化为 NULL 的优点是,可以确保在将指针设置为有用的值之前取消引用指针会崩溃,这实际上是一件好事,因为它可以防止代码在掩盖严重错误的同时“意外”工作。
The line:
is definitely not guaranteed to initialize the pointer value to anything in particular.
The line:
Will initialize the pointer to point to address zero, which in practice will never hold anything useful, and which will be conventionally checked for as an invalid pointer value.
Of course, it is still possible, as has been said by Doug T., to attempt to use this pointer without checking it and so it would crash all the same.
Explicitly initializing to NULL has the advantage of ensuring that dereferencing the pointer before setting it to something useful will crash, which is actually a good thing, because it prevents the code from "accidentally" working while masking a serious bug.
如果由于任何原因在声明发生时无法初始化指针,则最好将其初始化为 NULL。例如:
通常,函数可以检查指针的值是否为 NULL,以验证指针之前是否已初始化。如果您没有将其显式设置为 NULL,并且它指向一个随机值,那么它可能会被取消引用,从而导致段错误。
It's alway better to initialize a pointer to NULL if for any reason you can't initialize it while declaration occurs . For example:
Typically a function can check the value of the pointer against NULL to verify that the pointer has been initialized before. If you haven't set it explicitly to NULL, and it points to a random value, then it could be dereferenced causing a segfault.
C++ 继承了 C,它并不是被设计成安全的;它被设计为高效的。因此,自动变量不会被初始化。您需要确保在初始化之前不使用任何指针(尽管如果您不初始化变量,许多编译器会警告您)
C++ follow on from C in that it is not designed to be a safe; it is designed to be efficient. It is therefore for this reason that automatic variables are not initialized. It is up to you to ensure that no pointer is used before it is initialized (although many compiler will warn you if you don't initialize your variables)
如果不使用指针,编译器将忽略它。恕我直言,将其初始化为 NULL 是安全的做法。
您确定您没有与函数声明混淆吗? 是很常见的
将函数声明为char* do_something(const char* one,const char* Two);
。在这种情况下,指针用于指定要传递的参数类型。
If the pointer is not used, the compiler will simply ignore it. Initializing it to NULL is the safe thing to do, imho.
Are you sure your not confusing with a function declaration? It's very common for a function to be declared as
char* do_something(const char* one,const char* two);
In this case, the pointers are used to specify what kind of argument you want to pass.
现在
在上面的代码中可能有两种情况:
如果 ptr 中的默认值不是程序某些已使用内存的地址,它将执行。
输出:
<前><代码> ptr *ptr
例如。 0x400730 -1992206795
如果 ptr 中的默认地址是程序某些已使用内存的地址,则会给出错误(段错误)。例如,如果变量a在内存中的地址也是0x400730。
now
In the above code two cases can be possible:
It'll execute if default value in ptr is not the address of some used memory of program.
Output:
It'll give error (segmental fault) if default address in the ptr is the address of some used memory of program. E.g. if the address of variable a in the memory is also 0x400730.
在 C++ 中,通常应该完全避免使用普通的旧指针。标准库类、智能指针(C++0x 之前仅在 Boost 或 Loki 等各种库中)和引用可以而且应该在大多数地方使用。
如果你无法避免指针,确实最好用初始化来声明它们,在大多数情况下不应该为 NULL,而是实际的目标值,因为在 C++ 中你可以自由地混合声明和表达式,所以你可以而且应该只声明当您对其具有有意义的值时的变量。
C 的情况并非如此,您必须大量使用指针,并且所有变量都必须(或必须在 C99 之前;我不太确定)在作用域的开头声明。很多人仍然保留着 C 语言中不适合 C++ 的坏习惯。
In C++, you should generally avoid plain old pointers altogether. Standard library classes, smart pointers (until C++0x only in various libraries like Boost or Loki) and references can and should be used in most places instead.
If you can't avoid pointers, it's indeed preferable to declare them with initializations, which in most cases should not be NULL, but the actual target value, because in C++ you can mix declarations and expressions freely, so you can and should only declare the variable at the point you have meaningful value for it.
That's not the case with C where you have to use pointers a lot and all variables have to (or had to before C99; I am not exactly sure) be declared at the begining of a scope. So many people still have bad habits from C that are not appropriate for C++.