Size of struct allocation

Asked

Viewed 657 times

1

When declaring a normal variable of type int for example, the compiler goes there and, depending on the architecture, separates 4 bytes.

struct exemplo{
  int x;
  int b;
};

int main(){
   struct exemplo exemplo;
}

When I declared the variable exemplo in main the compiler allocates to stack everything in struct exemplo. The right would not be in the variable exemplo allocate the size of struct, which in that case would not be 8 bytes?

And in the case of dynamic memory allocation, when I allocate a type int she allocates 4 bytes address, but when I allocate a struct all variables of the struct. It wouldn’t have to be just the space of struct on the pointer?

1 answer

3

The allocation size of a structure is not so simple, there is the question of alignment. Depending on the compiler, directives, code and the platform you are running may be different but there is a reasonable chance it is 8 bytes even, just don’t count on it as being right. This can be discovered by the code.

#include <iostream>
using namespace std;

struct Exemplo { int x; int b; };

int main() {
    Exemplo exemplo;
    cout << "Tamanho de exemplo: " << sizeof(exemplo) << endl;
    cout << "Valor de exemplo.x: " << exemplo.x << endl;
    cout << "Valor de exemplo.b: " << exemplo.b << endl;
    Exemplo *exemplo2 = new Exemplo;
    cout << "Tamanho de exemplo2: " << sizeof(exemplo2) << endl;
    cout << "Tamanho do objeto apontado por exemplo2: " << sizeof(*exemplo2) << endl;
    cout << "Valor de exemplo2 (o ponteiro): " << exemplo2 << endl;
    cout << "Valor de exemplo2->x: " << exemplo2->x << endl;
    cout << "Valor de exemplo2->b: " << exemplo2->b << endl;
    cout << "Tamanho de exemplo2.x: " << sizeof(exemplo2->x) << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Dynamic allocation allocates the memory needed for the object you intend to allocate. It doesn’t matter if it is in the stack or in the heap, the size should be the same. In case it will allocate space to an object, in your example it would have 8 bytes and the variable will receive the pointer for this object. The size of the pointer is fixed to the application, all pointers have the same size, what varies is the size of the object it points to. Pointer and the object it points to are distinct things that have an eventual relation. In the case the object that would be pointed to must have 8 bytes, after all would be the same structure.

In a simple example the object would be in the heap and the pointer allocated in the variable would be in the stack. But nothing prevents the pointer from being on heap also just prefer to put code there for some reason.

If you want to understand more about pointer, there’s a question on the subject here on the website.

  • Vlw face more I want to know even because one allocated only memory addresses and another allocates the variables being that it would not have to be only the size?

  • I’m trying to understand what you’re talking about, but it’s hard. Try to read carefully what you write later and see if you think it’s understandable. Everything you have posted on the site so far is difficult to understand. Perhaps this is why you also do not understand what is written in the answers. No address allocations, let alone addresses and no variable allocations. Have you read all the responses you received here? You have read the links pointed out in the answers? You voted for only one of them and have not accepted any answers so far, have a look at your questions and see the [tour].

  • 1

    If I understand what you mean, Alexsander, you think using malloc the compiler allocates "only memory addresses", and making static statement it allocates "the variables". In fact, as @bigown has already explained, in both ways the compiler at all times allocates the space of the structure (ie "the variables"). Only when you use malloc (and consequently makes the allocation in the heap), the compiler allocates also a fixed size space for the pointer, which will literally point to the original space where "the variables are".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.