문제

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));
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top