C++ C4700:未初始化的局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21917424/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:52:27  来源:igfitidea点击:

C4700: uninitialized local variable

c++console

提问by Ben

When I compile this code it says "error C4700: uninitialized local variable 'b' used".I'm not sure what I have to do now to fix this problem. I'm neither an IT student or technican but I very like to learn C++ and I'm learning it by myself. I've been on this for 1 day.

当我编译这段代码时,它说“错误 C4700:使用了未初始化的局部变量‘b’”。我不确定我现在必须做什么来解决这个问题。我既不是 IT 学生也不是技术人员,但我非常喜欢学习 C++,而且我正在自学。我已经在这个上 1 天了。

Many thanks

非常感谢

#include <stdio.h>
#include <iostream>

//A. 
//1--
void InputArray(int *a, int &n)
{
    printf("Insert n = ");
    scanf("%d", &n);
    a = new int[n];
    for (int i=0; i<n; i++)
    {
        printf("Enter the key's a[%d] values: ", i);
        scanf("%d",&a[i]);
    }
}


void main()
{
    int *b, m;
    InputArray(b, m);
}

回答by Luchian Grigore

bis passed by value, which means a copy will be made, but since it's not initialized, you get the warning. Simply initialize it:

b是按值传递的,这意味着将创建一个副本,但由于它未初始化,因此您会收到警告。简单地初始化它:

int *b = nullptr;

or

或者

int *b = NULL;

回答by Mike Seymour

If you want the function to modify the caller's variable, then pass by reference:

如果您希望函数修改调用者的变量,则通过引用传递:

void InputArray(int *&a, int &n)
                     ^

Your version passes the uninitialised pointer by value; the function modifies a local copy of it, but leaves bin its uninitialised state.

您的版本按值传递未初始化的指针;该函数修改它的本地副本,但保持b未初始化状态。

回答by 4pie0

The pointers are not default initialized, so your variable bis uninitialized, this is the source of error. You have to initialize this variable to fix this:

指针未默认初始化,因此您的变量b未初始化,这是错误的来源。你必须初始化这个变量来解决这个问题:

void main()
{
    int *b = NULL, m;
    InputArray(b, m);
}

After you fix this there is additional problem in your code. It seems from the way you call a function that you expect to persistently change pointer bpassed into it, so that bwill point into memory allocated with newafter function returned. But you pass a pointer by value what means changes made in function will not be reflected in original variable bwhich will still point to what it pointed before the call to a function. (the array will be allocated inside function and will stay in memory after function returned but you will leak this memory as bwon't point into it). To fix this you have to pass pointer by reference:

修复此问题后,您的代码中还有其他问题。从您调用函数的方式看来,您希望持久地更改b传递给它的指针,以便b指向new在函数返回后分配的内存。但是您按值传递一个指针,这意味着在函数中所做的更改不会反映在原始变量中b,原始变量仍将指向它在调用函数之前指向的内容。(数组将在函数内部分配,并在函数返回后保留在内存中,但您将泄漏此内存,因为b它不会指向它)。要解决此问题,您必须通过引用传递指针

void InputArray(int*& a, int& n)

Also: where is delete? Remember: mapping newto deleteis bijection: every newcorresponds to single deleteplaced somewhere in code.

还有:在哪里delete?请记住:映射newdelete是双射:每个new对应于单个delete放置在代码中的某处。

回答by Billy Duguay

First of all, did you learn how to use an pointer correctly ? because if you know how to use pointer u should know that when you declare a pointer you need to be initialized to NULL before you can use it, correct me if i'm wrong.

首先,你学会如何正确使用指针了吗?因为如果您知道如何使用指针,您应该知道,当您声明指针时,您需要先将其初始化为 NULL,然后才能使用它,如果我错了,请纠正我。

Example

例子

int *b = nullptr;
int *b = NULL;
int *b = 0;
int *b(0);

It's all the same thing but in an different way

都是一样的东西,但方式不同