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..开发者_如何学Python.
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.
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.
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;
}
Your calling main function should then pass a value which can be modified:
int main()
{
double dbls[] = { 1, 2, 3, 4, 5 };
double* pArray = dbls;
// ...
someFunc(pArray, length);
// ...
for(int i = 0; i < length; i++)
{
std::cout << pArray[i] << ", ";
}
// ...
}
Ignoring the memory leak issue that results:
void someFunc(double* & pDoubleArray, int length)
// pass by reference ^^^ the pointer
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.
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:
void someFunc(double* pDoubleArray, int length)
{
for(int i = 0; i < length; i++)
{
pDoubleArray[i] = i * 3 + 2;
}
}
Or return the new array from the function:
// this indicates that the returned value is
// actually a new instance
double* getNewArray(double* pDoubleArray, int length)
{
double* pNewDoubleArray = new double[length];
for(int i = 0; i < length; i++)
{
pNewDoubleArray[i] = i * 3 + 2;
}
return pNewDoubleArray;
}
An alternative is to pass the input array by reference, but that complicates releasing unused instances.
[Edit]
To clarify this last case:
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;
}
void main()
{
double dbls[] = { 1, 2, 3, 4, 5 };
double* pArray = dbls;
// this will change what pArray
// points to
someFunc(&pArray, 5);
return 0;
}
As I've commented before, the latter approach will lead to memory leaks if pArray
points to a heap allocated array before calling someFunc
.
精彩评论