Lion.24
Lion.24

Reputation: 37

sizeof whole struct with member

Sorry for that dumb question but how can I know the actual size of a structure with severals members in it.

For example here is my struct :

typedef struct mastruct mastruct;
struct mastruct{
    int val;
    char *chaine;
    float decimal;
}__attribute__((packed));

and the affectation :

mastruct hello =
{
    23,
    "Hello world!",
    3.141592654
};

printf return me this :

printf("int : %d, char* : %d, float : %d\n", sizeof(int), sizeof(char*), sizeof(float));
printf("struct size : %d\n", sizeof(hello));

int : 4, char* : 8, float : 4
struct size : 16

which is normal because int + char* + float = 16 bytes but this is not what I want. I want to know the complete size of the structure in memory.

For example if I'm writing "Hello world!!!!!!!!!!!!!!!!!!!!!!!" instead of "Hello world!" this would modify the footprint memory and I need to know that.

With this example if I'm printing the size of the struct I still get 16 bytes :

mastruct hello =
    {
        23,
        "Hello world! Hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
        3.141592654
    };

Upvotes: 0

Views: 677

Answers (4)

tipaye
tipaye

Reputation: 474

First you'll have to somehow manage the memory for chaine safely. If you're allocating it directly using the malloc family, then you'll already know the size. If you're going to point it to a char array on the stack, then again you already know the size (use sizeof or strlen if in doubt). The point is that you have to look for 2 sizes: 1. Your struct, including (sizeof char *) 2. The actual string pointed to by chaine Please keep in mind that it is very unlikely your char array and struct will be contiguous, so you can't use this final size for indexing.

Upvotes: 1

abligh
abligh

Reputation: 25199

Your code is performing correctly. It is listing the size of the struct. The struct contains:

  • An int (4 bytes)
  • A char * (4 bytes on 32 bit, 8 bytes on 64 bit)
  • A float (here 4 bytes)

You thus get the total of 16 bytes. It's thus doing what you asked it to do.

If you want the size of the struct and the size of the things pointed do by the struct, use

sizeof(hello) + strlen(hello.chaine) + 1

the + 1 is for the terminating zero. Note though that multiple struct entries might point to the same string (depending on how you have things set up), so this won't necessarily represent the total memory allocated in respect of this item (only). Equally it might point to a constant string, in which case no (dynamic) memory will be allocated.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

sizeof operator returns the size of it's operand. In this case, the operand is hello which is of type struct mastruct, so it will produce the size of the structure itself, that is, the size of the member variables, plus padding (if any).

There is no way, it can measure the size of the memory location pointed to by a pointer type member variable.

Also, FWIW, without being a structure member also, sizeof will return the type of the variable itself (size of a pointer type), not the amount of memory allocated, for a pointer variable.

Upvotes: 0

SLaks
SLaks

Reputation: 888223

A char* is a pointer. It's always 8 bytes (or whatever size pointers are in your architecture; on 32-bit systems, it'll be 4 bytes). The actual string data lives elsewhere, and is not "owned" by the struct in any way.

You need to learn about pointers and memory management.

Upvotes: 4

Related Questions