2
using namespace std;
#include<iostream>
int main()
{
struct node
{
    int data;
    struct node *next;
};

struct node *node1;

node1 = (struct node*)malloc(sizeof(node));

node1->data = 5;
node1->next = NULL;

cout<<"node data value "<<node1->data<<endl;

int *vara;

cout<<"size of struct node pointer with * "<<sizeof(*node1)<<endl; // size is 8
cout<<"size of struct node pointer without * "<<sizeof(node1)<<endl; // size is 4
cout<<"size of integer pointer variable with * "<<sizeof(*vara)<<endl; // size is 4
cout<<"size of integer pointer variable with * "<<sizeof(*vara)<<endl; // size is 4 in this case as well

}

Why is there some difference in the size when used with * operator and without * operator for a pointer pointing to a struct variable ?

Executed the above code in CodeBlocks, Language C++.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
livetolearn
  • 221
  • 3
  • 11
  • `*node1` is not a pointer, it has a type of `struct node` (since you dereferenced it with `*`, therefore you are not comparing the sizes of pointers, but size of a pointer to size of a struct, which is apples to oranges kind of comparison. – Algirdas Preidžius May 05 '17 at 12:36
  • 1
    _"If we go by your logic, then I think no question can be asked on C++"_ The logic wasn't "no question can be asked on C++". It was "no question should be asked on C++ if it can be easily self-answered by simply reading an introductory tutorial or book". We're not here to teach the basics to every 5,000 new C++ visitors each day :) – Lightness Races in Orbit May 05 '17 at 12:48
  • @BoundaryImposition I get the point. Will follow your suggestion. Thanks – livetolearn May 05 '17 at 12:51

4 Answers4

2

Short answer: because node1 is a pointer and *node1 is a node, and these have different sizes.


Longer answer:

Let's examine the type of each of the expressions you are passing to the sizeof operator:

  1. *node1 has type node, which consists of an int and a node*, both of which has size 4 bytes on your platform, hence making the total size 8 bytes.
  2. node1 has type node*, which is a pointer. On your platform, pointers are 4 bytes long.
  3. *vara has type int, which is an integer. On your platform, integers are 4 bytes long.
  4. vara has type int*, which is a pointer. On your platform, pointers are 4 bytes long.
You
  • 22,800
  • 3
  • 51
  • 64
1

The first sizeof returns the size of the struct (the size of an int + the size of a pointer) , the second one the size of a pointer to a struct (4 bytes on your machine) the third one the size of an integer.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
0

It is because a pointer, in this case, has the same size (4 bytes) than an integer, but your node structure's size is 8 bytes. When you ask for sizeof(a) when a is a pointer, you ask for the size of the pointer. When you ask for sizeof(*a), you ask for the size of what is pointed by a.

Silveris
  • 1,048
  • 13
  • 31
0

Why is there some difference in the size when used with "" operator and without "" operator for a pointer pointing to a struct variable ?

Because they are different types. In this declaration:

node *ptr;

ptr has type pointer to node, while *ptr has type node. In your third and forth example looks like you wanted to compare int vs int *. The fact that you get the same size for int * and int is just a coincidence and happened to be the same on your platform. You cannot either rely on that nor deduce any rules from that fact.

Slava
  • 43,454
  • 1
  • 47
  • 90