1

As the question implies,what's the size of usual node:

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

If the struct holds the data and the address of next node, what's the size of the pointer address(not what it points to)? Is there a difference if the next points to NULL?

where is the data(which is not a pointer) saved, stack or heap, if I made "new node":

node* linkednode= new node;

Are the answers change if I made class node?

Tito Tito
  • 248
  • 3
  • 10

2 Answers2

1

You can easily check those using the sizeof() function applying it not only to your int and pointer variables but for the node itself. Usually ints and pointers are 4 bytes on 32 bit platforms, so this is implementation defined and might vary on other platforms. The total size of node might be equal to 8 but might not due to the cause specified above and\or data alignment.

Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
0

The size of a structure is fixed, no matter what the member values are. In this case it's probably either 8 or 16 bytes (depending on if you are on a 32 or 64 bit platform). This will be reported if you check e.g. sizeof(node).

The difference in size depending on 32 or 64 bit platforms, is because the pointer. Pointers are 32 bits (i.e. 4 bytes) on 32 bit platforms, and 64 bits (i.e. 8 bytes) on 64 bit platforms. The int type is usually 32 bits (4 bytes) on all platforms, but may be different on embedded systems or mainframes.

The reason it's not 12 bytes (4 + 8) on 64 bit platforms is because of alignment. The compiler will make sure that the pointer is on a good alignment to speed up access to that member.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621