[英]Can C++ pass empty pointer to a function?
For example, I want to declare pointer to array and to don't wanna initialize it.例如,我想声明指向数组的指针并且不想初始化它。 Then, I want pass it to a function and initialize it exactly in function.
然后,我想将它传递给 function 并在 function 中完全初始化它。 How can I do it in C++?
我该如何在 C++ 中做到这一点?
void F(int *B, const int& N) {
B = new int[N];
for (int i = 0; i < N; ++i) B[i] = i;
}
int main() {
int N = 4;
int *B = nullptr; // doesn't work
F(*B, N);
for (int i = 0; i < N; ++i)
cout << B[i] << endl;
return 0;
}
It evokes/produces error corresponds to the problem that B has no reference to point and, therefore he can't initialize array.它引发/产生错误对应于 B 没有引用点的问题,因此他无法初始化数组。
error: Process finished with exit code 11错误:进程以退出代码 11 结束
The pointer you pass is a copy of the original pointer.您传递的指针是原始指针的副本。 When you modify
B
in F
, B
i main()
remains the same.当您在
F
中修改B
时, B
i main()
保持不变。
You need to pass the pointer by reference or pass a pointer to the pointer:您需要通过引用传递指针或将指针传递给指针:
void F(int*& B, const int& N) {
B = new int[N];
for (int i = 0; i < N; ++i) B[i] = i;
}
int main() {
int N = 4;
int *B = nullptr; // doesn't work
F(B, N); //no operator here, just pass the pointer by reference
for (int i = 0; i < N; ++i)
cout << B[i] << endl;
return 0;
}
or或者
void F(int** B, const int& N) {
*B = new int[N];
for (int i = 0; i < N; ++i) (*B)[i] = i;
}
int main() {
int N = 4;
int *B = nullptr; // doesn't work
F(&B, N); //need address-of operator
for (int i = 0; i < N; ++i)
cout << B[i] << endl;
return 0;
}
Also, since you decided to use manual memory management, you are also responsible to release the memory acquired with new
.此外,由于您决定使用手动 memory 管理,您也有责任发布
new
获得的 memory。 This is not necessary if you use std::vector
as your container instead of raw pointer (or std::unique_ptr
or other containers).如果您使用
std::vector
作为容器而不是原始指针(或std::unique_ptr
或其他容器),则不需要这样做。
for (int i = 0; i < N; ++i)
cout << B[i] << endl;
delete[] B; //release memory after use
return 0;
You have at least two problems.你至少有两个问题。
The first in in the call where you use *B
.您使用
*B
的通话中的第一个。 *B
is the same as B[0]
, which means you're attempting to pass a single int
element to the function. *B
与B[0]
相同,这意味着您正在尝试将单个int
元素传递给 function。 But since B
is a null pointer, the dereference will lead to undefined behavior .但是由于
B
是一个 null 指针,取消引用将导致未定义的行为。
Correct (to fit the shown function signature) would be plain B
:正确(以适合显示的 function 签名)将是纯
B
:
F(B, N);
The second problem is that passing arguments to functions by default is done by value .第二个问题是默认情况下将 arguments 传递给函数是按值完成的。 Which means that the value of
B
is copied into the local variable B
inside the function F
.这意味着
B
的值被复制到 function F
内部的局部变量B
中。
When you assign to B
in the function you only change the local copy, and the original value in the main
function will be unmodified.当您在 function 中分配给
B
时,您只需更改本地副本, main
function 中的原始值将不会被修改。
To solve this you need to pass B
by reference :要解决这个问题,您需要通过引用传递
B
:
void F(int*& B, const int N) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.