243 views

as we allocate the space for node in linked list using malloc() so how many bytes malloc allocate for the 1 node i.e. actual value of malloc allocates in ram like i write this code.

so what is size of a node of linked list?

#include<stdio.h>

struct node
{
int data;

};

int main()
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
printf("%d",sizeof(struct node));

}

what the printf prints and why ?


Considering the size of pointer variable takes $8$ bytes and size of integer variable $4$ bytes.

Then the answer of $sizeof(struct node)$ would come $16$ .

This is because of padding added to satisfy alignment constraints. Alignment is very much important for performance and optimization of the program.

Here we try to make the structure size multiple of maximum size datatypes Here that is the pointer which is 8 byte.

So We pad 4 byte to int variable .

So now size of structure is $(8+8)=16$ byte

https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member

Ok

Thanks

But how you practice like either you are practice from hackerrank, codeforces like websites or where from you practice specially c programming?

Yes competitive coding in these sites.
Ok thank you very much

Doubt is clear now

1 vote