> n; a = new int-6ren">
gpt4 book ai didi

c++ - 指针错误 - "using uninitialized value"即使我认为它已初始化?

转载 作者:行者123 更新时间:2023-12-03 07:11:55 25 4
gpt4 key购买 nike

我说的是这段代码:

#include <iostream>

using namespace std;

void citire(int& n, int* a)
{
cin >> n;
a = new int[100];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}

int main()
{
int n, *a;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << *a << " ";
}
return 0;
}
我收到一条错误消息,说 a未初始化,我似乎无法理解为什么。我正在使用 a = new int[100]在那个函数里面,和 a是一个指针,所以我在哪里初始化它并不重要(只要我在使用它的值之前做)。
如果我在函数调用 citire(n, a) 之前添加该行在 int main() ,代码将起作用。
我可能认为产生错误的唯一原因是初始化发生在函数中。所以,如果我不调用 citire(n, a) ,然后 a将未初始化。这是错误的原因吗?
或者为什么我会得到它?
编辑:
出现混淆是因为我知道数组基本上是指针,并且在将数组作为参数传递时,如下所示:
#include <iostream>

using namespace std;

void citire(int& n, int a[])
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}

int main()
{
int n;
int a[100];
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << *a << " ";
}
return 0;
}
我不需要通过引用传递它——创建了一个浅拷贝。
为什么默认情况下使用数组会发生这种情况,但不会使用指针? (虽然现在很清楚为什么我的程序崩溃了。)

最佳答案

我修复了您的代码并打印了一些地址以供您理解。
旧代码:

#include <iostream>

using namespace std;

void citire(int& n, int* a)
{
cin >> n;
a = new int[100];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
}

int main()
{
int n, *a;
cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << *a << " ";
}
return 0;
}
输出:
Address of a = 0x7ffee5ab6b90
Address of n = 0x7ffee5ab6b98
5
1 2 3 4 5
In citire Address of a = 0x7ffee5ab6b50
In citire Address of n = 0x7ffee5ab6b98
0 0 0 0 0
注意变量的地址 anmain()和里面 citire()功能。
修改后的代码:
#include <iostream>

using namespace std;

void citire(int &n, int* &a)
{
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
}

int main()
{
int n, *a;

cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;

citire(n, a);

for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}

delete [] a;
return 0;
}
输出:
Address of a = 0x7ffee0eb1b90
Address of n = 0x7ffee0eb1b98
5
1 2 3 4 5
In citire Address of a = 0x7ffee0eb1b90
In citire Address of n = 0x7ffee0eb1b98
1 2 3 4 5
注意在这种情况下, na的地址在函数内部和 main 中是相同的。 .因为他们是 passed by reference ,实际变量(只是别名)在其内存位置完好无损的情况下传递,对它们所做的任何更改也反射(reflect)在 main也是。
关键要点:pointers通过 value到函数,除非你强制它通过 reference !
编辑(数组代码):
#include <iostream>

using namespace std;

void citire(int& n, int a[])
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "In citire Address of a = " << &a <<endl;
cout << "In citire Address of n = " << &n <<endl;
cout << "In citire Address of first element of a = " << &a[0] <<endl;
}

int main()
{
int n;
int a[100];
cout << "Address of a = " << &a <<endl;
cout << "Address of n = " << &n <<endl;
cout << "Address of first element of a = " << &a[0] <<endl;
citire(n, a);
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
return 0;
}
输出:
Address of a = 0x7ffeef1cda00
Address of n = 0x7ffeef1cd9f8
Address of first element of a = 0x7ffeef1cda00
5
1 2 3 4 5
In citire Address of a = 0x7ffeef1cd9b0
In citire Address of n = 0x7ffeef1cd9f8
In citire Address of first element of a = 0x7ffeef1cda00
1 2 3 4 5
请注意,如果是 array s,即使它们是按值传递的,在某种意义上, a 的地址函数调用的变化,区域的地址(即,静态声明的 100 个整数),基本上是数组第一个元素的地址( &a[0] ) 不要改变 .可以这样想:
  • 你有一个黑盒子,里面存储了一个数字,地址是a[0]。这是实际区域a点在。
  • 现在说黑匣子本身有一个识别号XXX .
  • 在函数调用过程中,你把黑盒子的内容 XXX ,进入另一个黑匣子,比如标识号 YYY .
  • 不懂YYY具有相同的指向区域,即 &a[0] .
  • 您运送新的 YYY黑盒函数使用和操作从 &a[0] 开始的区域中的数据.

  • 所以你得到的结果反射(reflect)在 main() .另请注意 arraypointer类型为 不是 相同的。

    关于c++ - 指针错误 - "using uninitialized value"即使我认为它已初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64749666/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com