-2

If the size of a structure pointer is 4 or say 8 bytes, how can it properly allocate the required memory for its data members in Dynamic Memory Allocation. This is my code:

#include<stdio.h>
typedef struct node{
   char a[10];
   char b[10];
   char c[10];
   int f;
   struct node* next;
}node;

int main()
{
   node* temp;
   int d= sizeof(node *);
   printf("Size of structure pointer = %d",d);
   temp= (node*)malloc(sizeof(node*));
   int c = sizeof(temp);
   printf("Size of Temp = %d\n",c);
}
```````````````````
/*
Here both Temp and node* is having a size of 8 bytes. 
But the size of the structure 'node' is 38 bytes(total size of the members). 
So how can the pointer Temp allocate that much memory if its size is just 8 bytes ?
*/
Aby
  • 13
  • 3
  • 1
    The size of a pointer is always the size of a pointer, and it's the size of the pointer itself and not what it points to. Also note that the result of `sizeof` is of type `size_t`, which you should print with the `%zu` format. What you want is `sizeof *temp` or possibly `sizeof(node)`. – Some programmer dude Nov 18 '19 at 07:42
  • 2
    And you really need to read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Because the program you show will *not* work on a 64-bit system. – Some programmer dude Nov 18 '19 at 07:43
  • The pointer does not `save` into memory the value of the structure. It only has the address of the variable you'll be saving. However, by declaring the pointer, the compiler will know it is the address of a specific type, thus allocating enough space for that variable – PhoenixBlue Nov 18 '19 at 07:44
  • 1
    Also please read [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member), because I really doubt your structure is 38 bytes even on a 32-bit system. – Some programmer dude Nov 18 '19 at 07:45

4 Answers4

1

Because you don't store data in pointers. Pointers point at data allocated elsewhere, hence the name "pointer". sizeof(node*) is wrong and the size of the pointer itself is irrelevant.

You should do

temp = malloc(sizeof *temp); // equivalent of sizeof(Node)
...
free(temp);
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

This is the pointer to the node struct, you don't need to allocate space for it. It can point to the node structure.

struct node *node_ptr;

If you want to store node structure at some pointer, you need to allocate space for the node structure NOT THE POINTER

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

You don't need to cast the result from the malloc, it is bad { see casting for malloc }

Nouman Tajik
  • 433
  • 1
  • 5
  • 18
0

Pointer is just the pointer. It you are a tourist guide in the war museum and use the pointer to show the tank - what is the weight of the pointer in your hand? The weight of the wooden stick or the weight of the tank?

0___________
  • 60,014
  • 4
  • 34
  • 74
0

First lets understand about malloc: Malloc is a function which allocates memory in a HEAP part of RAM. If we define a variable, usually it gets allocated in STACK part of RAM.

temp = (int*)malloc(n*sizeof(int));

this code allocates a memory in HEAP.

Now lets understand about ponters: consider the following code.

int a;
int *p;
p=&a;

in the about line of code the pointer 'p' is storing the address of the variable 'a'. Depending on the word length of your system(32bit or 64bit), the size of the pointer variable 'p' changes. It can be 2bytes or 4bytes or 8bytes. 8bytes is because in 64bit machine, "long int" can takes 8bytes.

Now lets understand about both pointers with structure:

struct value{
int a;
int b;
char c;
};

Since above code is a definition of structure, memory won't get allocated.

struct value s;

now the memory gets allocated in Stack.

struct value *s;
s = (struct value*)malloc(sizeof(struct value));

here the memory gets allocated in Heap. Even though 's' is structure pointer it still stores the address of the whole structure 'value', it's size will be 4/8. It points to whole structure not the particular member of structure.

The general structure 's' will have the data part of all its members. So the size will be 5 or 9 depending of the machine and the padding.

struct s{
    int   a; /* 4 bytes */
    char  b; /* 1 byte */
             /* 1 padding byte */
    short c; /* 2 bytes */
};

Refer Data Structure Padding

  • There are a number of issues in your answer, mostly about language and clarity. However, one that I think needs further explanation is this: *So the size will be 5 or 9...* (the subsequent code snippet shows a size of 8 bytes). – Adrian Mole Apr 19 '21 at 08:14
  • That subsequent code snippet shows example for data structure padding. It's not the example for "sizeof" structure. It's written that the size of structure depends also on the word size as well as data structure padding. – Vasukipriya K N Apr 20 '21 at 15:04