使用未初始化的指针作为函数参数

发布于 2024-11-05 08:56:05 字数 507 浏览 2 评论 0原文

当我想将未初始化的指针传递给函数时,它会出现运行时错误。 但如果我传递这个指针作为引用,它就可以正常工作。我无法解释为什么...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请爱~陌生人 2024-11-12 08:56:05

为了与 checkRef 版本等效,您的 check 函数应为:

void check(Body** b)
{
    *b = new Body();
}

并调用为

check(&b);

如果您不传递地址,就像在 check 中所做的那样(b),那么您将传递指针 b 的当前,该指针确实未初始化。

In order to be equivalent to the checkRef version, your check function should read:

void check(Body** b)
{
    *b = new Body();
}

and called as

check(&b);

If you don't pass the address, as you do in check(b), then you are passing the current value of the pointer b which is indeed uninitialised.

白馒头 2024-11-12 08:56:05

Body* b 不会将 b 设置为指向任何合理的内容。因此它可能指向某个随机位置,这可能会导致地球稍微移动。

将它传递给 check(通过指针的值)不会使其以任何方式初始化

void check(Body* b)
{
    b = new Body();
}

如果您通过指针传递它到指针,应该没问题

  void check(Body** b)
  {
     *b = new Body();
  }

并且调用

check(&b);

更好地以 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

void check(Body* b)
{
    b = new Body();
}

If you passed it by pointer to pointer, it should be okay

  void check(Body** b)
  {
     *b = new Body();
  }

And the call

check(&b);

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

命硬 2024-11-12 08:56:05

Body * &b 中,b 是 mainb 的别名/引用 - 因此,当您分配给它时,您会修改 < main 中的 code>b。在 Body* b 中,b 是 mainb本地副本,您正在修改此本地副本而不是 main 中的 b

您的运行时错误可能是由于在 main 中使用未初始化的 b 造成的。

编辑:您得到的实际错误是编译器嵌入到代码中的额外检查典型问题机制。它检测到您传递了未按值初始化的内容,这是没有意义的,因此会生成此错误。

如果您禁用此行为,应用程序将显示正常运行,直到您尝试取消引用 main 中未初始化的 b

In Body * &b b is an alias of/reference to the b in main - so when you assign to it you modify the b in main. In Body* b b is a local copy of the b in main, you're modifying this local copy and not the b in main.

Your runtime error is likely due to using the uninitialized b in main.

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 in main

情徒 2024-11-12 08:56:05

我不知道为什么您会看到运行时错误;我最多期望收到一个编译时警告/错误。但是您可能可以通过将 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 to NULL (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 in main(), so when you leave the function, there is no way to access the memory that was obtained by new.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文