简体   繁体   中英

What is the size of a structure pointer in C?

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 ?
*/

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);

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 }

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM