-1

I got confused with the size allocation with my gcc compiler, can any one help me how the size get allocated for the following code.

struct node
{
 int data;
 struct node *next;
};

sizeof(struct node) it gives an output as 16.


struct node
{
 int data;
};

sizeof(struct node) it gives an output as 4.


struct node
{
struct node *next;
};

sizeof(struct node) it gives an output as 8.


struct node
{
int data;
struct node *next;
}*link;

sizeof(link) is always 8, even if i add few more elements to structure.


Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
selvabharathi s
  • 137
  • 2
  • 8
  • 3
    read about struct padding and alignment,sizeof(link) is 8 because you check sizeof pointer, not the struct itself. sizeof(*link) will give you correct result. – Nick S May 29 '19 at 10:11

3 Answers3

1

On your specific platform, it looks like an int has size 4 and a pointer has size 8. It also looks like it wants to align pointers on an 8-byte boundary.

So if struct node just contains an int, then the its size is 4. If it just contains a pointer, its size is 8. If it contains both an int and a pointer, then it needs 12 bytes, but in order to maintain the alignment of the pointer, it pads the structure to a multiple of 8, resulting in a size of 16.

In your final example, you have link defined as a pointer to a struct node. In this case, it doesn't matter what a struct node contains. Since link is just a pointer, its size will always be 8.

Again, note that this is just a best guess for your platform. None of this is guaranteed, and it can vary from one platform to another.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • thank you i got a clear picture of your answer, but i struck in the place, where you said it pads the structure to multiple of 8. can you explain me in brief please. – selvabharathi s May 29 '19 at 10:46
  • @selvabharathis Structure sizes are padded to a multiple of the most restrictive alignment of any of the elements within them. For instance, if a structure contains an element that wants to be aligned on an 8-byte boundary, then the size will be padded to a multiple of 8. – Tom Karzes May 29 '19 at 10:51
  • @selvabharathis One reason for this is so that it can place multiple copies of the structure in an array, and have all of them be properly aligned. The elements of an array are placed directly adjacent to each other, so if the first array element is aligned on an 8-byte boundary, then its size must be a multiple of 8 to maintain 8-byte alignment for the next array element. – Tom Karzes May 29 '19 at 10:51
  • wow, i got it, thanks @tom-karzes – selvabharathi s May 29 '19 at 11:00
  • @selvabharathis Also note that if your structure contains an `int` followed by a pointer, it will need to place 4 bytes of padding *between* the two structure members in order to align the pointer. That way, if the structure is placed on an 8-byte boundary, the pointer will be properly aligned. So you need to take the sequence into account as well. – Tom Karzes May 29 '19 at 11:14
  • @selvabharathis For instance, if a structure contains an `int`, then a pointer, then another `int`, you might think it needs to be 16 bytes long, but in fact it would need to be 24 bytes: 4 for the first `int`, then 4 padding bytes, then 8 for the pointer, then 4 for the second `int`, and finally 4 more padding bytes to pad the size of the structure as a whole. So you get 4+4+8+4+4 = 24 bytes total. If instead the pointer was at the start or the end, and the ints were adjacent, then the size would only be 16. So order affects it. – Tom Karzes May 29 '19 at 11:16
  • @selvabharathis The answer that was listed as the duplicate goes into more detail about this. – Tom Karzes May 29 '19 at 11:18
0

sizeof(link) will always return 8 bytes because that's the size of a pointer in a 64 bits computer.

If you want the size of the structure you need to do sizeof(struct node) as it will give you the size of the actual struct.

And for a linked list type struct I would recomend you something like this

 typedef struct node Node, *pnode;
    struct node{
       //some vars
       pnode next;
    }

Then you'll just need to declare it your main or anywhere you as

pnode list = NULL;

sizeof(Node) //for memory allocation
0
  1. sizeof return size in bytes
  2. int have 32 bits = 4 bytes
  3. Any pointer char*, struct any* has const sizeof depends memory structure, in your case is 8.
  4. Because of memory alligment your first example has sizeof = 16, more here: https://en.wikipedia.org/wiki/Data_structure_alignment , you can add #pragma pack(1) and see diffrents, should be 4 + 8 = 12
Igor Galczak
  • 142
  • 6