0
votes

How do you make ai have the actual size of the structure that mptr is pointing to? In this case ai is returning size of the Object pointer which is 4 bytes. I am trying to get the actual structure size of the Object pointed by mptr

#include <iostream>    
using namespace std;

struct Object {
    int t;
    int g;
    float gBuffer;
};

int CreateObject() {
    Object* obj = new Object;
    return (unsigned int)obj;
}


int main() {
    unsigned int mptr = CreateObject();
    size_t ai = sizeof((Object*)mptr);

    cout << "Hello World" << endl; 
    return 0;
}
1
Never ever cast a pointer to an int! Or to an unsigned int that is then implicitly casted to an int... Use uintptr_t if you have to do something like this, but in this case there is no reason to do it at all. Just return an Object*.Max Truxa

1 Answers

2
votes

You can either "use the object itself" to get the size (note this all happens at compile time, so the compiler basically looks the type up for you).

Object* obj;
size_t size = sizeof(*obj);

Or use the type directly:

size_t size = sizeof(Object);

Advantage of the first way is that you can change the type of obj without having to search for every single sizeof you did on that variable through your whole codebase.

Full code, removing the cast to int:

#include <iostream>

using namespace std;


struct Object{
  int t;
  int g;
  float gBuffer;
};

Object* CreateObject(){
    return new Object;
}


int main(){
    Object* mptr = CreateObject();
    size_t ai = sizeof(*mptr); // or sizeof(Object)

    cout << "Hello World" << endl; 
    return 0;
}

Update:

It gets a bit more complicated once you try to get the size of a C++ standard library container.

Think about those containers like this:

struct container {
    size_t dataLength;
    char*  ptrToData;
};

As you already learned, the size of a pointer to an object is not equal to the size of the object itself.

sizeof(char*) -> 4 (32bit) or 8 (64bit) bytes
sizeof(char)  -> 1 byte

Likewise, the size of a container isn't the same as the size of the data it manages. An instance c of the container from above could point to a 1024 byte large char array, but sizeof(c) would still return 8 (on 32bit systems).

// Note that this is not 100% correct because of the padding
// the compiler could add to the struct, but this is out of the
// scope of this answer.
container c;
sizeof(c);                      // resolves to
sizeof(size_t) + sizeof(char*); // resolves to
4              + 4;             // 32bit

The size of the data pointed to by ptrToData would be stored in dataLength, so we could get the size by simply looking at the container.

Note that this totally differs from sizeof()

sizeof() returns the size of an object that is known at compile time. The C++ standard library containers are managing dynamic memory, which means there is no way the compiler (and hence sizeof()) can know how much memory you will allocate through the container at run time.

Most of the time there is no reason to know about the size of a container itself (actually if you think you need to know it, you are probably doing something wrong).

What you really need to know about, e.g., an std::vector is the number of elements it currently holds. For an std::vector you would get the number elements the container has allocated memory for by calling the std::vector<>::capacity() method and the actual number of elements currently in the container by calling std::vector<>::size().

Confused? Read about vectors here or just google it.