user4418808
user4418808

Reputation:

Self-Referential Structures

The self-referential structure is defined the following way:

struct node{
    int data1;
    int data2;
    struct node *ptr;
}obj,*temp;

int main()
{
     printf("\n Address of variable data1 in struct is %p",&obj.data1);
     printf("\n Address of variable data2 in struct is %p",&obj.data2);
}

The o/p is

Address of variable data1 in struct is 0xd0c7010
Address of variable data2 in struct is 0xd0c7018

which means that data1 is occupying 8 bytes of memory, right?

But if I have the following structure definition

struct node{
    int data1;
    int data2;
}obj,*temp;

int main()
{
     printf("\n Address of variable data1 in struct is %p",&obj.data1);
     printf("\n Address of variable data2 in struct is %p",&obj.data2);
}

The o/p is

Address of variable data1 in struct is 0xd0c6010
Address of variable data2 in struct is 0xd0c6014

So data1 is occupying 4 bytes of memory which an integer variable do, right?

But why in the first case is the memory space occupied by data1 increased?

EDIT: For the first case o/p is

Address of variable data1 in struct is 0x600940
Address of variable data2 in struct is 0x600944
The address of ptr is 0x600948
The size of struct is 16

For the second case

 Address of variable data1 in struct is 0x600910
 Address of variable data2 in struct is 0x600914
 The size of struct is 8

I am running this code on Linux using gcc (GCC) 4.1.2

The above code works fine but what about this below one

#include <stdio.h>
#include <stdlib.h>

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

// This function prints contents of linked list starting from the given node
void printList(struct node *n)
{
  printf("\n The memory location of n is %p ",n);
  while (n != NULL)
  {
     printf(" %d ", n->data);
     n = n->next;
     printf("\n The memory location of n in while loop is %p ",n);
  }
}

int main()
{
  struct node* head = NULL;
  struct node* second = NULL;
  struct node* third = NULL;

  // allocate 3 nodes in the heap
  head  = (struct node*)malloc(sizeof(struct node));
  second = (struct node*)malloc(sizeof(struct node));
  third  = (struct node*)malloc(sizeof(struct node));

  head->data = 1; //assign data in first node
  head->next = second; // Link first node with the second node
  printf("\n The memory address of head->data is %p ",&head->data);
  printf("\n The memory address of head->next is %p ",&head->next);

  second->data = 2; //assign data to second node
  second->next = third;
  printf("\n The memory address of second->data is %p ",&second->data);
  printf("\n The memory address of second->next is %p ",&second->next);

  third->data = 3; //assign data to third node
  third->next = NULL;

  printf("\n The memory address of third->data is %p ",&third->data);
  printf("\n The memory address of third->next is %p ",&third->next);

  printList(head);

  getchar();
  printf("\n");
  return 0;
}

O/P is

 The memory address of head->data is 0x215c010
 The memory address of head->next is 0x215c018
 The memory address of second->data is 0x215c030
 The memory address of second->next is 0x215c038
 The memory address of third->data is 0x215c050
 The memory address of third->next is 0x215c058
 The memory location of n is 0x215c010  1
 The memory location of n in while loop is 0x215c030  2
 The memory location of n in while loop is 0x215c050  3
 The memory location of n in while loop is (nil)

Why now there is still a difference of 8 bytes? I am running this code under the same environment as the other two.

Upvotes: 4

Views: 889

Answers (1)

P.P
P.P

Reputation: 121387

Struct padding is the reason for the difference you observe.

I think you are on a 64 bit systems where pointer size is 8 bytes (in the struct with a pointer). So the compiler aligns all the three members to 8 byte alignment. But in the later case, it's only two int's, so it's aligned to 4 bytes.

Upvotes: 1

Related Questions