-1

In Win32 Console Application (Visual C++) I have a array of objects and each object contains some other objects and variables, ex. (Owner and Equipment are structs, TimeInfo is class):

class Order
{
public:
    Order();
    ~Order();
    Owner owner;
    Equipment equipment;
    char *problem;
    TimeInfo timeinfo;
    void write();
    int order_number;
};

Next I have class OrderManager, that contains the array of this object:

items = (Order*)(malloc(100 * sizeof(Order)));

In program, I add and remove items, but what is the best way to free memory at the end of program? I have free(manager.items);, but this doesn't work.

tofro
  • 5,640
  • 14
  • 31
Peter
  • 43
  • 5

2 Answers2

0

As people have already pointed out, you should avoid using malloc in this situation. If you want to allocate 100 instances of Order, you could try something like this:

Order* items = new Order[100];

In this case the default constructor of Order is called for every instance.

When you want to free up the memory, you do:

delete[] items; // note the "[]"

Presumably, you want to handle the Orders within an OrderManager, so a typical place for the new/delete would be in the construtor/destructor of the OrderManager.

When using new and delete, the con- and destructors will be called. It is important that the con- and destructors are called, since they are typically used to set up and initialize things on construction and release things on destruction.

With malloc and free, all that happens is the allocation of memory on the heap.

In your question you mentioned that

free(manager.items);

does not work. Shouldn't you be freeing the pointer items in stead of manager.items?

ThorOdinsson
  • 252
  • 1
  • 2
  • 7
-1

I would use a destructor to manage freeing memory allocated in the OrderManager class.

You should be using C++ new and delete to allocate/deallocate memory and also should be using containers to store collections of an object.

Refer to this link for a list of options on how you can initialize the vector: https://stackoverflow.com/a/6143009/3817744

class OrderManager {
private:
     std::vector<Order> items;
public:
     OrderManager() {
         // Refer to the link above
     }
     ~OrderManager() {
         items.clear();
     }
}

Here is a link to an interesting post about C++'s new operator by David Mazières. http://www.scs.stanford.edu/~dm/home/papers/c++-new.html

Community
  • 1
  • 1
bicole
  • 11
  • 1
  • Please don't promote the usage of `malloc` in C++. And don't use C-style casts. And `new[]` is not the *"new C++ style"* - new C++ style would be to not manually allocate dynamic arrays but use containers for it, e.g.: `std::vector` – UnholySheep Jan 12 '17 at 15:54
  • 1
    The usage of `malloc` is wrong. `Order` is not a POD type, and using `malloc` to create 100 of them will not construct the `Order` objects. – PaulMcKenzie Jan 12 '17 at 15:56
  • Thanks, I'll update. – bicole Jan 12 '17 at 16:45
  • So yes, this is a reasonable answer when allocating memory you should use the class constructor, when freeing memory you should try and use the class destructor. This is the C++ way, and this is the RAII recommended way. – Owl Mar 22 '22 at 10:14