1

Consider the following code:

typedef struct list_ele
{
    char *value;
    struct list_ele *next;
}list_ele_t;

typedef struct
{
    list_ele_t *head;
    int qSize;
}queue_t;

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

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

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.

0130
  • 15
  • 7
  • 2
    An idiomatic way: `some_type *ptr = malloc(sizeof(*ptr));` Then, if `some_type` is ever changed, the statement "does the right thing" so no changes needed. – Craig Estey Jan 08 '23 at 18:05

2 Answers2

1

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

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

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:

sizeof(queue_t)    -> 16
sizeof(list_ele_t) -> 16

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

Thanos Kyprianos
  • 1,628
  • 2
  • 7
  • 13
1
    Stack         |       Heap
                  |
      q ----------|---> +------------+
                  |     + A chunk of +         
                  |     +   memory   +
                  |     +  equal to  +
                  |     +   sizeof   +
                  |     +   queue_t  +
(a pointer to     |     +------------+
 a struct of      |          / \
 type queue_t)    |         /   \ 
                  |        /     \  
                  |   qsize      head    
                  |                |
                  |                |
                  |                | 
                  |      +-----------+         
                  |      + Chunk of  +
                  |      + memory of +
                  |      + size list +
                  |      +-----------+
                  |

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.

Harith
  • 4,663
  • 1
  • 5
  • 20
  • Thanks! That's a very good picture of this problem! the head pointer contains "char *value", so that means If I want to store something in value I have to malloc the pointer of head and the pointer of value right? – 0130 Jan 10 '23 at 05:49
  • Surely, if you want to allocate the memory they point to on the heap. – Harith Jan 10 '23 at 05:54
  • Oh, thanks a lot, I think I know the problem of my program! – 0130 Jan 10 '23 at 05:57
  • Anytime. Consider accepting the answer if it helped you. – Harith Jan 10 '23 at 06:03