Question

struct list{
int a,b,c;
struct list *next, *previous;
}

Now I need to know the difference in memory allocated in the two scenarios:

1) struct list *ptr = (struct list*)malloc(sizeof(struct list*));

and

2) struct list *ptr = (struct list*)malloc(sizeof(struct list));
Was it helpful?

Solution

You have typed the same line twice. Both are same.

struct list *ptr = (struct list*)malloc(sizeof(struct list*));

Here you have allocated the memory whose size is sizeof(struct list*)

Here struct list * is a pointer to type struct list. so size of any pointer on 32 bit machine is 4 byte and on 64 bit machine its 8 byte.

so you have allocated that much memory.

I think in second option you may be want to ask about this

struct list *ptr = (struct list*)malloc(sizeof(struct list));

Here you have allocated memory whose size is the size of that struct list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top