使用未初始化的指针作为函数参数
当我想将未初始化的指针传递给函数时,它会出现运行时错误。 但如果我传递这个指针作为引用,它就可以正常工作。我无法解释为什么...
class Body
{
};
void check(Body* b)
{
b = new Body();
}
void checkRef(Body* &b)
{
b = new Body();
}
int main001()
{
Body* b;
//check(b);// error: The variable 'b' is being used without being initialized. (in VS2010)
checkRef(b); // OK
return 0;
}
将 b 传递给 check 和 checkRef 时有什么区别?我在 VisualStudio2010 中收到运行时错误。错误:变量“b”正在使用但未初始化。
编辑:这是 VS2010 调试输出。发布版本中不会出现“错误”
As I want to pass an uninitialized pointer to a function, it goes runtime error.
but if I pass this pointer as a reference, it works OK. I cannot explain why...
class Body
{
};
void check(Body* b)
{
b = new Body();
}
void checkRef(Body* &b)
{
b = new Body();
}
int main001()
{
Body* b;
//check(b);// error: The variable 'b' is being used without being initialized. (in VS2010)
checkRef(b); // OK
return 0;
}
Whats the difference when b is passed to check and checkRef? I get the runtime error in VisualStudio2010. error:The variable 'b' is being used without being initialized.
EDIT: it was a VS2010 debug output. the "error" doesn't appear in release version
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了与
checkRef
版本等效,您的check
函数应为:并调用为
如果您不传递地址,就像在
check 中所做的那样(b)
,那么您将传递指针b
的当前值,该指针确实未初始化。In order to be equivalent to the
checkRef
version, yourcheck
function should read:and called as
If you don't pass the address, as you do in
check(b)
, then you are passing the current value of the pointerb
which is indeed uninitialised.Body* b
不会将 b 设置为指向任何合理的内容。因此它可能指向某个随机位置,这可能会导致地球稍微移动。将它传递给 check(通过指针的值)不会使其以任何方式初始化
如果您通过指针传递它到指针,应该没问题
并且调用
更好地以 C++ 方式完成,并使用您给出的参考示例,因为它更新了引用的指针值指向新分配的Body
Body* b
does not set b to point to anything sensible. So it may point to some random location which could cause the earth to shift a bit.Passing it to check(by value for the pointer) does not make it initialised in any way
If you passed it by pointer to pointer, it should be okay
And the call
Better done in C++ way with the reference example you give since that updates the referenced pointer value to point to the newly allocated Body
在
Body * &b
中,b 是main
中b
的别名/引用 - 因此,当您分配给它时,您会修改 <main
中的 code>b。在Body* b
中,b 是main
中b
的本地副本,您正在修改此本地副本而不是main
中的b
。您的运行时错误可能是由于在
main
中使用未初始化的b
造成的。编辑:您得到的实际错误是编译器嵌入到代码中的额外检查典型问题机制。它检测到您传递了未按值初始化的内容,这是没有意义的,因此会生成此错误。
如果您禁用此行为,应用程序将显示正常运行,直到您尝试取消引用
main
中未初始化的b
In
Body * &b
b is an alias of/reference to theb
inmain
- so when you assign to it you modify theb
inmain
. InBody* b
b is a local copy of theb
inmain
, you're modifying this local copy and not theb
inmain
.Your runtime error is likely due to using the uninitialized
b
inmain
.EDIT: The actual error you get is an extra check-for-typical-problems mechanism embedded into your code by your compiler. It detects that you pass something uninitialized by value, which doesn't make sense, and therefore generates this error.
If you disable this behavior, the application will appear to function properly until you try to dereference the uninitialized
b
inmain
我不知道为什么您会看到运行时错误;我最多期望收到一个编译时警告/错误。但是您可能可以通过将
b
初始化为NULL
来修复它(但这不会修复您的程序;见下文...)第一个函数和第二个函数之间的区别调用是第一个将导致内存泄漏。您正在按值传递指针,因此在
check()
中您正在使用该指针的副本。您分配了这个副本,但这不会影响main()
中指针的值,因此当您离开该函数时,无法访问获得的内存新的。
I don't know why you're seeing a run-time error; at most I would expect to receive a compile-time warning/error. But you can probably fix it by initialising
b
toNULL
(but this won't fix your program; see below...)The difference between the first and the second function call is that the first one will cause a memory-leak. You are passing the pointer by-value, so inside
check()
you are working with a copy of that pointer. You assign this copy, but this doesn't affect the value of the pointer inmain()
, so when you leave the function, there is no way to access the memory that was obtained bynew
.