简体   繁体   English

C++类指针动态数组释放问题

[英]C++ Class pointer dynamic array deallocate problem

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define ROW 1

class Foo
{
public:
    Foo()
    {
        this->dummy = new unsigned int[100];
    }
    ~Foo()
    {
        delete[] this->dummy;
        this->dummy = NULL;
    }

    unsigned int* dummy;
};

Foo** allocate()
{
    Foo** foo_array = NULL;
    foo_array = new Foo * [ROW]; //Create space for Foo addresses (row)

    for (int i = 0; i < ROW; i++)
        foo_array[i] = new Foo; //Create and allocate Foo for each address space(col)

    return foo_array;
}

int deallocate(Foo* foo_array[ROW])
{
    if (foo_array != NULL)
    {
        for (int i = 0; i < ROW; i++)
            delete foo_array[i];
        delete[] foo_array;
        
        foo_array = NULL;

        return 1;
    }

    return 0;
}

void main()
{
    Foo** foo_array = NULL;

    foo_array = allocate();
    deallocate(foo_array);
    
    if (foo_array != NULL)
        printf("not null something wrong\n");
    system("pause");
}

In main() function, foo_array should be pointed to NULL as soon as the deallocation is performed by the deallocate(Foo* foo_array[ROW]) function.在 main() 函数中,一旦由 deallocate(Foo* foo_array[ROW]) 函数执行解除分配, foo_array 就应该指向 NULL。

but, In deallocate(Foo* foo_array[ROW]) function,但是,在 deallocate(Foo* foo_array[ROW]) 函数中,

foo_array = NULL;

It seems point to NULL by above syntax, however in main() function, foo_array is not point to NULL.上面的语法似乎指向 NULL,但是在 main() 函数中, foo_array 并不指向 NULL。

so, I tried to change above syntax in deallocate(Foo* foo_array[ROW]) function,因此,我尝试在 deallocate(Foo* foo_array[ROW]) 函数中更改上述语法,

foo_array = NULL; => (*foo_array) = NULL;

It spits out write access violation errors.它会吐出写访问冲突错误。

Where did it go wrong?哪里出错了?

The correct syntax would be foo_array = deallocate (foo_array);正确的语法是foo_array = deallocate (foo_array); and your deallocate should return NULL .并且您的deallocate应返回NULL

I don't see any reason to declare foo_array as pointer to pointer.我看不出有任何理由将foo_array声明为指向指针的指针。 In your code you are passing pointers by value.在您的代码中,您按值传递指针。 That means you can change what the pointers are pointing at but you can't change the values of the pointers.这意味着您可以更改指针指向的内容,但不能更改指针的值。 You can solve the problem using references.您可以使用引用来解决问题。

#include <cstdio>

#define ROW 1

class Foo
{
public:
    Foo()
    {
        this->dummy = new unsigned int[100];
    }
    ~Foo()
    {
        delete[] this->dummy;
        this->dummy = NULL;
    }

    unsigned int *dummy;
};

void allocate(Foo *&foo_array)
{
    foo_array = new Foo[ROW];
}

void deallocate(Foo *&foo_array)
{
    delete[] foo_array;
    foo_array = nullptr;
}

int main()
{
    Foo *foo_array = nullptr;

    allocate(foo_array);
    deallocate(foo_array);
    
    if (foo_array != nullptr)
        printf("not null something wrong\n");
}

Of course you can make your code much simpler using STL containers or smart pointers当然,您可以使用 STL 容器或智能指针使您的代码更简单

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

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