简体   繁体   English

如何在两个不同的函数中访问动态数组C ++

[英]How to access a dynamic array in two different functions c++

I need to access a dynamic array in two different functions. 我需要使用两个不同的函数访问动态数组。 The changes made to it in one need to transfer over to the other. 一个方面对它所做的更改需要转移到另一个方面。

These are the functions: 这些是功能:

void populate(int size, int *ptr)
{
    ptr = new int[size];
    for (int i = 0; i < size; i++)
    {
        ptr[i] = rand() % 51;
    }
}

void display(int size, int *ptr)
{
    for (int i=0; i < size; i++)
    {
        cout << ptr[i] << endl;
    }
}

it is called in the main as 它主要被称为

int* ptr = NULL;

In populate , you are attempting to make a pointer you pass to the function point to a dynamically allocated array. populate ,您尝试使传递给功能点的指针指向动态分配的数组。 But you pass the pointer by value . 但是您通过传递指针。 This has no effect on the caller side, and results in a memory leak. 这对调用方没有影响,并导致内存泄漏。 You need to pass either the pointer by reference: 您需要通过引用传递任何一个指针:

void populate(int size, int*& ptr)
                            ^

or return it 或退回

int* populate(int size)
{
   int* ptr = new int[size];
   ....
   return ptr;
}

But the easiest and safest thing to do would be to use an std::vector<int> for both functions instead. 但是最简单,最安全的方法是对两个函数都使用std::vector<int> For example 例如

std::vector<int> populate(size_t size)
{
    std::vector<int> v(size);
    for (auto& i : v)
    {
        i = rand() % 51;
    }
    return v;
}

void display(const std::vector<int>& v)
{
  for (auto i : v)
  {
    std::cout << ptr[i] << std::endl;
  }
}

This way, it is clear what is being returned, and the caller doesn't have to read up on whether they have to manage the resources pointed at by a raw pointer. 这样,很明显将返回什么,并且调用方不必读取是否必须管理原始指针指向的资源。

Note that populate can be replaced by a call to std::generate , and display by a call to std::copy . 请注意, populate可以由对std::generate的调用替换,并可以由对std::copy的调用display

You have to remember that when passing arguments to a function, they are passed by value , meaning they are copied. 您必须记住,在将参数传递给函数时,它们是通过value传递 ,这意味着它们已被复制。 So when you pass a pointer to a function, the pointer is copied, and when you modify that pointer (like eg ptr = new int[size] ) you only modify the local copy. 因此,当您将指针传递给函数时,将复制该指针,并且在修改该指针(例如ptr = new int[size] )时,仅修改本地副本。

In C++ you can pass arguments by reference which means that you can modify the arguments and their changes will be reflected in the calling function: 在C ++中,您可以通过引用传递参数这意味着您可以修改参数,并且它们的更改将反映在调用函数中:

void populate(int size, int*& ptr)

A simple solution is to make the first function return the pointer and store it in another variable. 一个简单的解决方案是使第一个函数返回指针并将其存储在另一个变量中。 You can then send it to the second function. 然后,您可以将其发送到第二个功能。

usually when you need to pass a pointer to a function which does the initialization of the pointer, you either pass p* ointer to pointer * or the function returns a pointer . 通常,当您需要将指针传递给执行指针初始化的函数时,可以将p * ointer传递给指针 *或该函数返回指针

The easiest way to do it is to simply return a pointer from the populate function: 最简单的方法是从填充函数中返回一个指针:

int* populate(int size, int *ptr)
{
    ptr = new int[size];
    for (int i = 0; i < size; i++)
    {
        ptr[i] = rand() % 51;
    }
    return ptr;
}

 int*p = populate(N,p);   


display(int size, int *ptr)
{
    for (int i=0; i < size; i++)
    {
        cout << ptr[i] << endl;
    }
}

The reason why simply passing the pointer does not work is that the pointer is passed by value, which means a local copy of the pointer is created locally in your function ; 简单地传递指针不起作用的原因是,指针是通过值传递的,这意味着在函数中本地创建了指针本地副本 that local copy is being initialized and destroyed when you exit the function 退出功能时,该本地副本正在初始化和销毁

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM