C++指针数组
代码
#include "stdafx.h"
#include <iostream>
void someFunc(double* pDoubleArray, int length)
{
double* pNewDoubleArray = new double[length];
for(int i = 0; i < length; i++)
{
pNewDoubleArray[i] = i * 3 + 2;
}
pDoubleArray = pNewDoubleArray;
}
int main()
{
double dbls[] = { 1, 2, 3, 4, 5 };
int length = sizeof dbls / sizeof dbls[0];
std::cout << "Before..." << std::endl;
for(int i = 0; i < length; i++)
{
std::cout << dbls[i] << ", ";
}
std::cout << std::endl;
someFunc(dbls, length);
std::cout << "After..." << std::endl;
for(int i = 0; i < length; i++)
{
std::cout << dbls[i] << ", ";
}
std::cout << std::endl;
while(true){ }
return 0;
}
输出
Before...
1, 2, 3, 4, 5,
After...
1, 2, 3, 4, 5,
这是我想要做的: 1. 创建一个数组并用一些值填充它 2. 将该数组作为指针传递给一个函数,该函数将创建一个新数组并将传入的数组重新分配给新创建的数组 3. 打印更改
虽然我没有看到任何更改,但我不知道为什么。
Code
#include "stdafx.h"
#include <iostream>
void someFunc(double* pDoubleArray, int length)
{
double* pNewDoubleArray = new double[length];
for(int i = 0; i < length; i++)
{
pNewDoubleArray[i] = i * 3 + 2;
}
pDoubleArray = pNewDoubleArray;
}
int main()
{
double dbls[] = { 1, 2, 3, 4, 5 };
int length = sizeof dbls / sizeof dbls[0];
std::cout << "Before..." << std::endl;
for(int i = 0; i < length; i++)
{
std::cout << dbls[i] << ", ";
}
std::cout << std::endl;
someFunc(dbls, length);
std::cout << "After..." << std::endl;
for(int i = 0; i < length; i++)
{
std::cout << dbls[i] << ", ";
}
std::cout << std::endl;
while(true){ }
return 0;
}
Output
Before...
1, 2, 3, 4, 5,
After...
1, 2, 3, 4, 5,
Here's what I am trying to do:
1. Create an array and fill it with some values
2. Pass that array as a pointer to a function that will create a new array and reassign the one that was passed in to the newly created array
3. Print out the changes
I am not seeing any changes though, and I do not know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的函数 someFunc 的接口是错误的。它应该需要指针地址的引用(或指向指针的指针),以便您可以返回新数组的地址。否则,您只是修改本地值。
然后,您的调用 main 函数应该传递一个可以修改的值:
The interface of your function someFunc is wrong. It should require the reference of a pointer's address (or the pointer to a pointer) so that you can return the address of your new array. Otherwise, you are simply modifying a local value.
Your calling main function should then pass a value which can be modified:
忽略导致的内存泄漏问题:
Ignoring the memory leak issue that results:
线路 pDoubleArray = pNewDoubleArray;分配指针的本地副本
通过引用传递指针、向其传递指针或返回新值
我的偏好是返回新值,这是一个样式问题。
The line pDoubleArray = pNewDoubleArray; assigns the local copy of the pointer
Either pass the pointer by reference, pass a pointer to it, or return the new value
My preference would be to return the new value, bit that's a style issue.
目前尚不清楚为什么要将旧数组传递给不使用它的函数。
如果您要更改单个值,则创建新的数组实例是没有意义的。如果没有,则只需创建一个新数组并返回它。
因此,要么更改原始数组:
要么从函数返回新数组:
另一种方法是通过引用传递输入数组,但这会使释放未使用的实例变得复杂。
[编辑]
澄清最后一种情况:
正如我之前评论过的,如果
pArray
在调用someFunc
。It is unclear why you are passing the old array to a function which does not use it.
If you are changing individual values, then there is no point in creating a new array instance. If not, then simply create a new array and return it.
So, either change the original array:
Or return the new array from the function:
An alternative is to pass the input array by reference, but that complicates releasing unused instances.
[Edit]
To clarify this last case:
As I've commented before, the latter approach will lead to memory leaks if
pArray
points to a heap allocated array before callingsomeFunc
.