stackunderflow
stackunderflow

Reputation: 3883

Memory allocation for objects in arrays

so this is my code [ notice that I commented the cstr and destructor]

#include <iostream>
#include <stdlib.h>
#include <array>

class MyIntClass
{
    int _mymember;

  public:  

// MyIntClass(){}
// ~MyIntClass(){}

void *operator new(size_t size)
{
    std::cout << "new: Allocating " << size << " bytes of memory" << std::endl;
    void *p = malloc(size);

    return p;
}
void operator delete(void *p)
{
    std::cout << "delete: Memory is freed again " << std::endl;
    free(p);
}


void *operator new[](size_t size)
{
    std::cout << "new: Allocating " << size << " bytes of memory" << std::endl;
    void *p = malloc(size);

    return p;
}

  void operator delete[](void *p)
    {
        std::cout << "delete: Memory is freed again " << std::endl;
        free(p);
    }
};

void line(){
        std::cout << "\n--------------------------------------------------\n" << std::endl;
}

int main()
{
    line();
    std::cout << "Using new overloading and malloc\nWe will create one object of MyIntClass that is supposed to be 4 bytes" << std::endl;
    MyIntClass *m1 = new MyIntClass();

    line();
    //I want to create an array of the MyIntClass of two objects
    std::cout << "Now we create array of MyIntClass using <array> header" << std::endl;
    std::array<MyIntClass, 2> z = {};

    std::cout << " The elements in the array z = "<< z.size() <<std::endl;

    std::cout << "The memory allocated for  array z = " << sizeof(z) << std::endl;

    line();

    std::cout << "\nNow we create array using new[] overloading and malloc " << std::endl;

    MyIntClass *i = new MyIntClass[2]();
    delete[] i;
}

now the result is as follow:


Using new overloading and malloc
We will create one object of MyIntClass that is supposed to be 4 bytes

new: Allocating 4 bytes of memory


Now we create array of MyIntClass using <array> header
The elements in the array z = 2
The memory allocated for array z = 8


Now we create array using new[] overloading and malloc
new: Allocating 8 bytes of memory

To me as inexperienced C++ programmer I think every thing is working as expected

Now If I uncomment the constructor the same result will happen

however when I uncomment the destructor different result will occur

Now we create array using new[] overloading and malloc
new: Allocating 12 bytes of memory

So my question is what is the explanation for this: creating array of two objects each is 4 bytes will result in 8byte memory allocation for the array in both methods using the array library or the overloading the new[] and malloc.

however when we have a destructor for the object the malloc will allocate 12 bytes not 8 bytes for this array of 2 elements.

I saw this question on SO but it didn't explain my case

following is my compiler version :

gcc version 8.2.0 (MinGW.org GCC-8.2.0-3)

Upvotes: 0

Views: 855

Answers (1)

Andrey Semashev
Andrey Semashev

Reputation: 10644

The reason for extra allocated memory is that the compiler needs to know the number of elements in the array in order to be able to call destructors on each element of the array when you call delete[]. For trivially destructible types the compiler doesn't need to call destructor, so the extra space for the array size is not needed.

Upvotes: 2

Related Questions