2

I found this code on geeksforgeeks while studying about new operator overloading:

#include<iostream>
#include<stdlib.h>
 
using namespace std;
class student
{
    string name;
    int age;
public:
    student()
    {
        cout<< "Constructor is called\n" ;
    }
    student(string name, int age)
    {
        this->name = name;
        this->age = age;
    }
    void display()
    {
        cout<< "Name:" << name << endl;
        cout<< "Age:" << age << endl;
    }
    void * operator new(size_t size)
    {
        cout<< "Overloading new operator with size: " << size << endl;
        void * p = ::operator new(size);
        //void * p = malloc(size); will also work fine
     
        return p;
    }
 
    void operator delete(void * p)
    {
        cout<< "Overloading delete operator " << endl;
        free(p);
    }
};
 
int main()
{
    student * p = new student("Yash", 24);
 
    p->display();
    delete p;
}

In the output it is showing that the number of bytes of memory to be allocated for the object is 40 bytes. The size of string datatype is 32 bytes and the integer is 4 bytes then shouldn't it be 36 bytes ?

  • What value are you expecting? Also, this might help you: https://stackoverflow.com/questions/55478523/how-does-stdvector-support-contiguous-memory-for-custom-objects-of-unknown-siz – NathanOliver Sep 21 '21 at 15:26
  • An obvious answer would be "the size of `string` plus the size of `int`". But this is too obvious, probably not answering your question! If so, please explain what you really want to ask. You can [edit] your question to clarify it. – anatolyg Sep 21 '21 at 15:26
  • I have edited the question – Tushar Bharti Sep 21 '21 at 15:29
  • Why should 36 plus 4 be 36, and not 40? – NathanOliver Sep 21 '21 at 15:29
  • @NathanOliver the sizeof(string) was coming out to be 32 bytes not 36... – Tushar Bharti Sep 21 '21 at 15:30
  • Woops. Missed that. See this: https://stackoverflow.com/questions/58435348/what-is-bit-padding-or-padding-bits-exactly/58436082#58436082 – NathanOliver Sep 21 '21 at 15:32
  • I get 40 for a 64-bit build and 28 for a 32-bit build. The size of a `std::string` is 32 and 24, respectively. So there's 4 bytes of padding before the `string` member in the 64-bit build ... to 8-byte align a pointer member thereof, I expect. – Adrian Mole Sep 21 '21 at 15:33
  • @NathanOliver Why would there be padding shouldn't the memory allocation be just 32 bytes for the string and 4 bytes for the int as 4 is also a multiple of 32? – Tushar Bharti Sep 21 '21 at 15:34
  • But 36 is not a multiple of `8` (64 bits) which appears to be what you are using. – NathanOliver Sep 21 '21 at 15:34
  • Try inverting the order of the `int` and `string` members ... that may give you your missing 4 bytes back (or it may not). – Adrian Mole Sep 21 '21 at 15:35
  • @NathanOliver Okay I think I got it, thanks! – Tushar Bharti Sep 21 '21 at 15:35
  • If you compile this in `gcc` and use the `-Wpadded` switch the compiler will warn you that it's adding `4` bytes of padding for this particular case. – WBuck Sep 21 '21 at 15:36
  • @AdrianMole No it was still 40 bytes – Tushar Bharti Sep 21 '21 at 15:36

0 Answers0