Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some useful examples of malloc() in C?

Tags:

c

malloc

I'm just reading about malloc() in C.

The Wikipedia article provides an example, however it justs allocate enough memory for an array of 10 ints in comparison with int array[10]. Not very useful.

When would you decided to use malloc() over C handling the memory for you?

like image 713
alex Avatar asked Nov 03 '10 07:11

alex


People also ask

What is malloc in C with example?

The malloc() function stands for memory allocation, that allocate a block of memory dynamically. It reserves the memory space for a specified size and returns the null pointer, which points to the memory location. malloc() function carries garbage value. The pointer returned is of type void.

What for malloc () is used?

Malloc is used for dynamic memory allocation and is useful when you don't know the amount of memory needed during compile time. Allocating memory allows objects to exist beyond the scope of the current block.

What is malloc used for in C?

C malloc() method The “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form.

What is malloc and calloc in C with example?

malloc() function creates a single block of memory of a specific size. calloc() function assigns multiple blocks of memory to a single variable. 2. The number of arguments in malloc() is 1.


1 Answers

Dynamic data structures (lists, trees, etc.) use malloc to allocate their nodes on the heap. For example:

/* A singly-linked list node, holding data and pointer to next node */
struct slnode_t
{
    struct slnode_t* next;
    int data;
};

typedef struct slnode_t slnode;

/* Allocate a new node with the given data and next pointer */
slnode* sl_new_node(int data, slnode* next)
{
    slnode* node = malloc(sizeof *node);
    node->data = data;
    node->next = next;
    return node;
}

/* Insert the given data at the front of the list specified by a 
** pointer to the head node
*/
void sl_insert_front(slnode** head, int data)
{
    slnode* node = sl_new_node(data, *head);
    *head = node;
}

Consider how new data is added to the list with sl_insert_front. You need to create a node that will hold the data and the pointer to the next node in the list. Where are you going to create it?

  • Maybe on the stack! - NO - where will that stack space be allocated? In which function? What happens to it when the function exits?
  • Maybe in static memory! - NO - you'll then have to know in advance how many list nodes you have because static memory is pre-allocated when the program loads.
  • On the heap? YES - because there you have all the required flexibility.

malloc is used in C to allocate stuff on the heap - memory space that can grow and shrink dynamically at runtime, and the ownership of which is completely under the programmer's control. There are many more examples where this is useful, but the one I'm showing here is a representative one. Eventually, in complex C programs you'll find that most of the program's data is on the heap, accessible through pointers. A correct program always knows which pointer "owns" the data and will carefully clean-up the allocated memory when it's no longer needed.

like image 183
Eli Bendersky Avatar answered Oct 06 '22 00:10

Eli Bendersky