Mr_t
Mr_t

Reputation: 37

How is memory allocated, when using structures

I'm curious, how exactly does memory allocation looks like after calling struct list x; for code bellow:

struct list {
    int key; 
    char name[10];
    struct list* ptr;
};

Variable x will store 4 bytes for key, 10 bytes for name and how much bytes for ptr?

Upvotes: 0

Views: 83

Answers (4)

nanofarad
nanofarad

Reputation: 41281

struct list will be allocated as a single contiguous block of memory likely containing the following (assuming sizeof(int) == 4 for this platform and toolchain. I use the word "likely" because some considerations here are actually implementation-defined.

  • Four bytes for key.
  • Ten bytes for name.
  • Padding bytes to align ptr to the expected alignment.
  • sizeof(list*) bytes for a pointer. On a modern-day desktop computer using a common operating system and ABI (meaning a flat addressing model), I could guess that it's likely to be 4 or 8 bytes for 32-bit and 64-bit systems respectively. In reality, a pointer's size is implementation-defined and depends on a number of factors, as Eric Postpischil adds:

    ...the C standard permits pointers to be different sizes depending on the types they point to. For example, in old word-addressed computers, some pointers may have only a word number. To address characters, a C implementation had to synthesize byte addresses by adding extra bits to the address and generating extra instructions to manipulate the word data.

The size of the alignment is a bit tricky to figure out since it depends on a combination of the platform (different CPU architectures have different alignment requirements), toolchain/ABI, and any unusual commands/configurations (e.g. #pragma pack or equivalent).

If I had to guess with reasonable assumptions but no information, it would be plausible that there are two bytes of padding regardless of whether this was a 32-bit or 64-bit system. Two bytes of padding places the offset of ptr at 4+10+2=16, which satisfies both a four-byte and an eight-byte alignment.

Upvotes: 2

Ray Toal
Ray Toal

Reputation: 88378

It will be dependent on one's architecture but try it out

#include <stdio.h>

struct list {
    int key; 
    char name[10];
    struct list* ptr;
};

int main(void) {
    printf("Size of struct is %d\n", sizeof(struct list));
    struct list the_list;
    printf("struct is at address %p\n", &the_list);
    printf("key is at address %p\n", &the_list.key);
    printf("name is at address %p\n", &the_list.name);
    printf("ptr is at address %p\n", &the_list.ptr);
    return 0;
}

When I ran this I got

Size of struct is 24
struct is at address 0x7ffcf32ad210
key is at address 0x7ffcf32ad210
name is at address 0x7ffcf32ad214
ptr is at address 0x7ffcf32ad220

showing that of the 24 bytes total, the first 4 bytes were for key at the beginning of the memory block, the next 12 for name, and then the final 8 were for ptr. Notice there were 2 bytes of padding between name and ptr.

But this may differ on different architectures. As always, best to try things out!

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222901

Your structure will start with four bytes for key (because int is four bytes in your C implementation) and ten bytes for name. (The C standard would allow a C implementation to insert padding between key and name, but there is no need for it.)

Pointers are commonly four or eight bytes in modern systems, but they can be other sizes. They can even be different sizes in different C implementations on the same system. So ptr will likely take four or eight bytes in your C implementation.

Pointers may require alignment, such as requiring four-byte alignment for a four-byte pointer. In this case, there will be two bytes of padding after name and before ptr. That is because the structure has 14 bytes in key and name, so, to bring that up to a multiple of four, two bytes of padding are needed. (Again, the C standard allows an implementation to insert more padding, but it is not needed.)

Upvotes: 0

nir shahar
nir shahar

Reputation: 368

The size of a pointer is constant. It doesn't depend on the size of the structure it points to.

For additional information on the size of pointers, take a look here

Upvotes: 0

Related Questions