-2

I have a basic memory allocation related question. What would be the total sizes of the below structs in C?

typedef struct rubble
{
    int betty;
    char barney[4];
    struct rubble* bambam;
} rubble;

typedef struct{
    short* wilma[2];
    short fred[2];
    rubble dino;
}flinstone;

For rubble = betty( 4 bytes ) + barney ( 4 bytes ) + bambam ( ? ). What would be the size of the bambam pointer to rubble type?

For flinstone = wilma ( 4 bytes ? ) + fred ( 4 bytes ) + dino( sizeof(rubble) )

How do we calculate the size of pointer to type short in this case?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Htlcs
  • 545
  • 3
  • 13
  • 26
  • 2
    Depends on compiler and optimization level. – stark Sep 16 '14 at 02:23
  • Okay thanks but I was just confused like for bambam pointer to type rubble, would it account for just the pointer size or the whole rubble struct size? – Htlcs Sep 16 '14 at 02:25
  • 3
    @JimZilla a pointer points at things, it doesn't contain the things it is pointing at. – M.M Sep 16 '14 at 02:31
  • 1
    pointer to short or pointer to anything will always be of same size coz in essence they are addresses. this depends on the address space available for the program. – Haris Sep 16 '14 at 02:32
  • 1
    sizeof(int*) = sizeof(char*) = sizeof(long*) = sizeof(rubble*). See where I'm going with this? – enhzflep Sep 16 '14 at 02:32
  • 3
    @enhzflep: Note that pointers do not all need to be the same size. http://stackoverflow.com/questions/6751749/size-of-a-pointer – Bill Lynch Sep 16 '14 at 02:41
  • 1
    @sharth: Hm, those answers don't touch on pointers to objects of different types not needing to be of the same size. Not that relevant for modern / mainstream systems though, so nearly correct. – Deduplicator Sep 16 '14 at 02:45
  • @sharth - could you provide me an example that demonstrates this to be the case? I'd not considered microcontrollers, but anything with an OS these days uses a flat memory model and, as such, unless I'm mistaken, the size of a pointer to a _data_ object is the same, regardless of type. – enhzflep Sep 16 '14 at 02:56
  • 2
    @enhzflep: Here's an example: http://stackoverflow.com/a/399999/47453 – Bill Lynch Sep 16 '14 at 03:17
  • Thanks @sharth. Gawd, installing and running that IDE that was an awful experience, you've reminded me of some of the reasons I uninstalled this IDE in '07. So in short, compilers that are non-standard (the far keyword is non standard) may do other things. What a walk down memory lane (pardon the pun) haven't thought of Phar Lap and/or Dos4GW in eons. I guess OpenWatcom's implementation is something of a hangover from those days. Thanks for the interesting and illuminating insight. :thumbsup: – enhzflep Sep 16 '14 at 06:30

1 Answers1

1

So. I'm going to make some assumptions.

One of these is that all pointers have the same size. I'm also going to assume that you're on a 64 bit platform, which means that all of your pointers are probably 8 bytes long.

Additionally, I'll assume an int is the same as int32_t. And a short is an int16_t. And that char is int8_t, and that there are 8 bits in a byte.

typedef struct rubble
{
    int betty;             // offset = 0  size = 4
    char barney[4];        // offset = 4  size = 4
    struct rubble* bambam; // offset = 8  size = 8
} rubble;

So the total size here is likely going to be 16.

typedef struct{
    short* wilma[2];       // offset = 0  size = 16
    short fred[2];         // offset = 16 size = 4
    rubble dino;           // offset = 24 size = 16
} flinstone;

For a total size of 40.

Now, note that dino starts further into flinstone then you might imagine. This is because rubble needs 8 byte alignment.

Now, it's also possible that the alignment needs are different for different platforms, and the types could have different sizes as well. But this is a possible outcome.

It's also possible that structs will have padding at the end of them. For example:

struct x {
    uint32_t a; // offset = 0  size = 4
    uint8_t  b; // offset = 4  size = 1
};

The total size of x is 8, because we need to support the case where we have an array of x, and keep x[2].a aligned.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173