What will happen if I malloc a pointer of a struct datatype which contains an element of a pointer of another datatype?

huangapple go评论91阅读模式
英文:

What will happen if I malloc a pointer of a struct datatype which contains an element of a pointer of another datatype?

问题

考虑以下代码:

  1. typedef struct list_ele
  2. {
  3. char *value;
  4. struct list_ele *next;
  5. } list_ele_t;
  6. typedef struct
  7. {
  8. list_ele_t *head;
  9. int qSize;
  10. } queue_t;

如果我使用函数来为queue_t类型的指针分配内存,就像这样:

  1. queue_t *q = malloc(sizeof(queue_t));

实际上会发生什么?我的意思是,malloc将创建多大的内存,我是否应该使用以下代码:

  1. q->head = malloc(sizeof(list_ele_t));

来为q->head申请空间?

我不擅长C语言(;w;),我尽力思考了但什么也没得到。

英文:

Consider the following code:

  1. typedef struct list_ele
  2. {
  3. char *value;
  4. struct list_ele *next;
  5. }list_ele_t;
  6. typedef struct
  7. {
  8. list_ele_t *head;
  9. int qSize;
  10. }queue_t;

And If I use a function to malloc a pointer of queue_t like this

  1. queue_t *q = malloc(sizeof(queue_t));

What it actually happen? I mean, how big the memory it will create by malloc, should I use the following code

  1. q->head = malloc(sizeof(list_ele_t));

to apply for space for q->head?

I'm not good at C language (;w;) , I try my best to think but I got nothing.

答案1

得分: 1

一个C指针通常!!!是8个字节,所以当你执行以下操作时:

  1. queue_t *q = malloc(sizeof(queue_t));

你分配了8个字节(指针)+ 4个字节(整数)+ 4个字节(填充,见下文)(通常)的内存,然后你让q指向它。由于你还没有为q->head分配任何内容,q->head将指向随机的内存位置。当你执行以下操作时:

  1. q->head = malloc(sizeof(list_ele_t));

你分配了8个字节 + 8个字节(通常)的内存,然后将q->head指向它。此外,了解一下结构填充是什么:

  1. sizeof(queue_t) -> 16
  2. sizeof(list_ele_t) -> 16

因此,实际上你为q指向的内存分配了超过12个字节。

英文:

A C pointer is usually!!! 8 bytes so when you do

  1. queue_t *q = malloc(sizeof(queue_t));

you allocate 8(pointer) + 4(int) + 4(padding, see bellow) (usually) bytes of memory and you have q point to it. q->head is going to point to a random location as you have not assigned anything to it. Of course when you do

  1. q->head = malloc(sizeof(list_ele_t));

you allocate 8 + 8 (usually) bytes of memory and you point q->head to it. Also learn what structure padding is:

  1. sizeof(queue_t) -> 16
  2. sizeof(list_ele_t) -> 16

So you actually allocate more than 12 bytes for the memory that q points to.

答案2

得分: 1

  1. |
  2. |
  3. q ----------|---> +------------+
  4. | + 一块内存 +
  5. | + 大小等于 +
  6. | + queue_t +
  7. | + 结构体 +
  8. | +------------+
  9. (指向 queue_t | / \
  10. 结构体的指针) | / \
  11. | / \
  12. | qsize head
  13. | |
  14. | |
  15. | |
  16. | +-----------+
  17. | + 一块大小为 +
  18. | + size list +
  19. | + 的内存 +
  20. | +-----------+
  21. |

附注:

  1. 通过 malloc 分配的存储是未初始化的。
  2. malloc 通过返回 NULL 指针值来指示失败。
  3. 对于每次分配,都必须有一个对应的 free
英文:
  1. Stack | Heap
  2. |
  3. q ----------|---> +------------+
  4. | + A chunk of +
  5. | + memory +
  6. | + equal to +
  7. | + sizeof +
  8. | + queue_t +
  9. (a pointer to | +------------+
  10. a struct of | / \
  11. type queue_t) | / \
  12. | / \
  13. | qsize head
  14. | |
  15. | |
  16. | |
  17. | +-----------+
  18. | + Chunk of +
  19. | + memory of +
  20. | + size list +
  21. | +-----------+
  22. |

Side Notes:

  1. The storage that is allocated through malloc is uninitialized.

  2. malloc indicates failure by returning a NULL pointer value.

  3. For every allocation, there must be a free.

huangapple
  • 本文由 发表于 2023年1月9日 01:20:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定