函数在不删除动态数组的情况下工作。程序运行并且没有错误

Function works without deleting the dynamic array. Program works and has no errors

我是一名学生,也是C++的初学者。在class中,我们目前正在练习指针、动态数组和函数。我写了一个没有错误的程序,并且做了它必须做的事情。我的问题是它可以在不删除动态数组的情况下工作。 我们被要求将 delete[] 放在函数中

#include <iostream>
using namespace std;

char* Function (char c[], int numbers[]){
    int k=0;

    for ( int i=0; i< 4;i++){
        k+=numbers[i];
    }

    k++;
    char *result = new char[k];
    char *counter=result;

    for ( int i=0; c[i]!='[=10=]';i++)
    {
        int index = numbers[i];

        for (int j=0; j< index; j++)
        {
            *counter=c[i];
            counter=counter+1;
        }
     }

     *counter='[=10=]';

     return result;

 }

int main ()
{
    char c[]="abcd";
    int numbers []={1,2,3,4};
    char d[]="ghj";
    int numbersx []={2,1,1};

    cout << Function(c,numbers);
    cout << Function(d,numbersx);

    return 0;   
 }

我不清楚在这种情况下 delete[] 的位置。 我搜索过 this recommends that i put the delete inside main 其他答案没有回答我的确切问题。

在函数内部删除就可以了。只是不要再 free/delete 了。您也可以在 main().

中删除它

如果不删除它就不会出现明显的问题(编译器不会报错,程序也不会崩溃)。然而,您将有内存泄漏,这绝对不是您想要的。

My problem is that it works without deleting the dynamic array.

它可能会工作...但它也会泄漏内存。内存泄漏在没有无限内存的计算机上是一件坏事。

It is unclear to me where the delete[] would be positioned in this case.

它的位置必须确保指向的对象在删除后永远不会使用这里使用意味着访问对象的内存,例如通过取消引用指向它的指针。

if I put it after the return will it still be deleted?

如果你的意思是它在 Function 但在 return 之后,那当然不是。 returning.

后函数中的任何内容都不会执行

It has to be placed in the function and not in main.

如果您要在 Function 中删除,那么您将 return 指向已删除对象的指针,这将非常糟糕。我建议放弃这个愚蠢的要求。

从技术上讲,main 永远不会取消对指针的引用,因此即使您确实在 Function 中删除,程序在技术上仍将具有明确定义的行为,即使 return 的值会然后就完全没用了。

如果不释放内存,最终会导致内存泄漏。对于像这样的简单应用程序,您不太可能发现问题。对于更大/更长的 运行ning 应用程序,在应用程序退出之前分配大量数据,您最终会 运行 内存不足并且程序会崩溃 and/or 您的操作系统将数据分页到磁盘时速度会变慢。

要释放内存但仍打印一些输出,您需要这样的东西:

#include <iostream>
using namespace std;

void Function (char c[], int numbers[]){
    int k=0;

    for ( int i=0; i< 4;i++){
        k+=numbers[i];
    }

    k++;
    char *result = new char[k];
    char *counter=result;

    for ( int i=0; c[i]!='[=10=]';i++)
    {
        int index = numbers[i];

        for (int j=0; j< index; j++)
        {
            *counter=c[i];
            counter=counter+1;
        }
    }

    *counter='[=10=]';

    cout << result;

    delete [] result;
 }

int main ()
{
    char c[]="abcd";
    int numbers []={1,2,3,4};
    char d[]="ghj";
    int numbersx []={2,1,1};

    Function(c,numbers);
    Function(d,numbersx);

    return 0;   
}

这是您在函数中获取 delete[] 的唯一方法,因为正如其他人所说,您仍然需要访问内存以在函数返回后报告结果。

另一种方法是在 main 方法中调用 delete [] 两次,如下所示:

#include <iostream>
using namespace std;

char* Function (char c[], int numbers[]){
    int k=0;

    for ( int i=0; i< 4;i++){
        k+=numbers[i];
    }

    k++;
    char *result = new char[k];
    char *counter=result;

    for ( int i=0; c[i]!='[=11=]';i++)
    {
        int index = numbers[i];

        for (int j=0; j< index; j++)
        {
            *counter=c[i];
            counter=counter+1;
        }
    }

    *counter='[=11=]';

    return result;
}

int main ()
{
    char c[]="abcd";
    int numbers []={1,2,3,4};
    char d[]="ghj";
    int numbersx []={2,1,1};

    char* result = Function(c,numbers);
    cout << result;
    delete[] result;

    result = Function(d,numbers);
    cout << result;
    delete[] result;

    return 0;   
}