-1

I am trying to implement a linked list. I am curious about how much space will this node structure take. I tried it with sizeof(node) and got 16 bytes but I had expected 12 bytes as int on my system is 4 bytes and a pointer is 8 bytes. Note that this is pseudocode.

/*a pseudo c++ file to demonstrate linked list*/
node{
   int val;
   node *p;
};

linkedlist{
   ...
   ...
};
aritra
  • 52
  • 6
  • But `node` *is* defined in the code sample you've shown. Not that your code *actually* compiles. Why don't you show the code that you're talking about? – cigien Jun 24 '20 at 16:40
  • Presumably you mean `struct node {` rather than just `node {`? – G.M. Jun 24 '20 at 16:43
  • You can always get the *size of* a struct by using `sizeof`. In your example, the size of the struct is: `sizeof(int) + sizeof(pointer)`. The sizes of `int` and pointer will vary depending on your platform and compiler. (The `int` type is based on *range*, not bit width.) – Thomas Matthews Jun 24 '20 at 16:50
  • 3
    BTW, your `node` structure contains a *pointer to a node*, not an instance of the `node`. If it were an instance, you'd have a nasty circular implementation (that goes on forever). Pointers may have a different size than the structure they point to. – Thomas Matthews Jun 24 '20 at 16:53
  • 1
    I vote to close this question because it is not reproducable after clarifiying the misconception that with the shown struct NOT in fact "an instance of it is used inside its definition". – Yunnosch Jun 24 '20 at 17:27
  • @Yunnosch i do not have any padding in my code so no padding should have been added let alone 4 bytes – aritra Jun 26 '20 at 06:41
  • Do you know what padding is? Padding explains the 4 bytes you can't account for. It ensures the pointer is 8-byte aligned. Are you using a compiler directive to specify no padding? – Blastfurnace Jun 26 '20 at 06:45
  • @Blastfurnace i said what i understood from the answer provided by Yunnosch – aritra Jun 26 '20 at 06:47
  • Your comment still doesn't make sense. Padding isn't something you put in your code. It's done by the compiler. Try this experiment. Exchange the order of `val` and `p` and see if the size of the struct changes. – Blastfurnace Jun 26 '20 at 06:49
  • I did not provide an answer. I did not mention padding. I close-voted as "not reproducable", which I explained by mentioning 'I vote to close this question because it is not reproducable after clarifiying the misconception that with the shown struct NOT in fact "an instance of it is used inside its definition".' The close vote reason given can be caused by the gold badge owner being the only one proposing the dupe, with or without the third one agreeing. A little confusing, I admit. – Yunnosch Jun 26 '20 at 08:13

1 Answers1

0

I think you have not declared your node correctly. You should declare it as below:

struct node{
   int val;
   node *p;
};

and to know the size you can do something like below in your main function:

int main()
{
   node n;

   cout << "size of node: " << sizeof (n) << endl;
}

in my system it shows the below output, As you have one int variable and one pointer in node, so in my system int is the size of 4 and the pointer's size depends on the int size because the size of a pointer is same as int, so 4 + 4 =8 :

size of node: 8
  • In my system an `int` is of size 4 bytes and a pointer is of size 8.So it should total 12 bytes but `sizeof(node)` is showing 16. – aritra Jun 25 '20 at 10:55
  • can you please post the output of sizeof(int), sizeof(int*) and sizeof(node)? the int and pointer is same in size. – Vishal Chaudhary Jul 16 '20 at 12:05