-5

I call delete in destructor but it say: undentifier "data" is undefined! Shouldn't delete work in the destructor?

struct Coada
    {
        Coada(int size_max=0)
        {
            int prim = -1;
            int ultim = -1;
            int *data = new int[size_max];
        }

        ~Coada()
        {
            delete[] data;
        }


    };
Mareș Ștefan
  • 430
  • 1
  • 4
  • 13
  • 7
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Mar 14 '18 at 12:33
  • 6
    You seem to have quite a few misunderstandings about C++. You should take a step back and systematically learn the language from a good book. This `new[]` and `delete[]` business is a bad idea to begin with, for instance. – Baum mit Augen Mar 14 '18 at 12:34
  • I used them wrong? – Mareș Ștefan Mar 14 '18 at 12:35
  • 2
    `data` is no more after constructor goes out of scope, it will not be available in destructor or anywhere else – Killzone Kid Mar 14 '18 at 12:36
  • 4
    The variables you think are part of your class are actually not. modern practice is to use `std::vector` or `std::unique_ptr` instead of raw pointers. Learning C++ without a good tutorial or book is suicide. – patatahooligan Mar 14 '18 at 12:36
  • Please learn the very basics of how to use variables and class members before posting a question. It is expected that you have done a minimum of research before posting on SO. – underscore_d Mar 14 '18 at 12:40
  • 2
    `data` is a local variable of the constructor, move it as a field of the struct. – Jean-Baptiste Yunès Mar 14 '18 at 12:50
  • related: http://en.cppreference.com/w/cpp/language/rule_of_three I strongly recommend you read that before attempting to override your destructor – UKMonkey Mar 14 '18 at 13:13

3 Answers3

1

To delete a pointer, the value of the pointer has to be stored until the point of deletion. Since the pointer data only exists until the constructor returns, and no copies are made of the pointer value, the pointer cannot be deleted after the constructor returns. Since it wasn't deleted before that, the allocated memory will have leaked.

Furthermore, a variable cannot be accessed outside of its scope. data is a local variable of the constructor, and cannot be accessed outside of that function. There is no variable data in the destructor; hence the error from your compiler.

So, if you do allocate something in a function, and don't wish to deallocate it within that function, you must store the pointer somewhere. Since the function where you allocate is a constructor, it would be natural to store the pointer in a member variable. The destructor can access member variables and would therefore be able to delete the pointer.

However, keep in mind that it is extremely rare for C++ programmer to need to do manual memory management. It should be avoided as much as possible. For example, in this case, it would be smart to use std::vector to allocate a dynamically sized array.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Everything the others say is correct and you should follow it.
To answer your question. Yes, delete should work in destructor if you do it right. Here is an example how it will work:

struct Coada{

        Coada(int size_max=0){
            int prim = -1;
            int ultim = -1;
            data = new int[size_max];
        }

        ~Coada(){
            delete[] data;
        }

    private:
        int* data;
    };

You can see I declare data as member variable of struct Coada so I can access it everywhere in this struct, also in destructor.

But all of them you will learn in a good c++ book.

Enjoy reading :)

Morchul
  • 1,987
  • 1
  • 7
  • 21
  • 1
    Then when `{Couda c1; Couda c2; c1 = c2;}` is attempted, another round of questions as to "why delete doesn't work?" will arise. – PaulMcKenzie Mar 14 '18 at 13:26
  • I don't know, but I'm sure you will answer these question. I'm pretty sure there are better ways to do this. This is only one easy example for him, how he can solve his problem. – Morchul Mar 14 '18 at 14:29
0

This should work using a class for your object :

class Coada {
private:
    int prim;
    int ultim;
    int *data;

public:
    Coada(int size_max=0)
    {
        this->prim = -1;
        this->ultim = -1;
        this->data = new int[size_max];
    }

    ~Coada()
    {
        delete[] this->data;
    }
};

int main(void)
{
    Coada my_coada(4);
    return 0;
}
ThomasRift
  • 98
  • 6