views:

224

answers:

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

How does the compiler allocate memory for "next" member in when we have not yet allocated memory for the structure "struct node"

+7  A: 

Next is only a pointer so it is a fixed size value in every machine, it'll just add int+pointer sizes + padding and allocate node struct

Arkaitz Jimenez
+17  A: 

The member next is a pointer. Pointers are all the same size, so the compiler does not need to know how big the thing that next may point to is.

anon
In other words, the compiler allocates enough space for "next" to store the pointer to node in the node structure. To actually use "next" you must make another allocation call to fill the next structure.
Christopher
+3  A: 

next member is a pointer - a variable that will contain an address of node, not node itself. All data type pointers are usually of the same size so it's enough for the compiler to know that it's a pointer to be able to compute its size.

sharptooth