C++指针数组

发布于 2024-11-07 08:49:44 字数 1107 浏览 0 评论 0原文

代码

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染年凉城似染瑾 2024-11-14 08:49:44

你的函数 someFunc 的接口是错误的。它应该需要指针地址的引用(或指向指针的指针),以便您可以返回新数组的地址。否则,您只是修改本地值。

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;
}

然后,您的调用 main 函数应该传递一个可以修改的值:

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] << ", ";
  }
  // ...
}

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] << ", ";
  }
  // ...
}
薔薇婲 2024-11-14 08:49:44

忽略导致的内存泄漏问题:

void someFunc(double* & pDoubleArray, int length)
// pass by reference ^^^ the pointer

Ignoring the memory leak issue that results:

void someFunc(double* & pDoubleArray, int length)
// pass by reference ^^^ the pointer
花落人断肠 2024-11-14 08:49:44

线路     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.

苦行僧 2024-11-14 08:49:44

目前尚不清楚为什么要将旧数组传递给不使用它的函数。

如果您要更改单个值,则创建新的数组实例是没有意义的。如果没有,则只需创建一个新数组并返回它。

因此,要么更改原始数组:

void someFunc(double* pDoubleArray, int length)
{
    for(int i = 0; i < length; i++)
    {
        pDoubleArray[i] = i * 3 + 2;
    }
}

要么从函数返回新数组:

// 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;
}

另一种方法是通过引用传递输入数组,但这会使释放未使用的实例变得复杂。

[编辑]

澄清最后一种情况:

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;
}

正如我之前评论过的,如果 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文