Understanding the purpose of malloc and calloc

I try to bow my head to C. Reading through K & R, I move back and forth, trying to find where it indicates the situations that I should get in dynamic memory blocks.

For example, I want to have an int pointer.

int *pointer; 

But then K & R states that I can do:

 int *pointer; pointer = (int*)malloc(sizeof(int)); 

Essentially what I did here is another? In the first case, I created a pointer that has not yet pointed to anything, so I assume that the computer runs out of memory, and I'm trying to point it to an int value. I will have problems. The second case reserves space for my pointer. Therefore, I do not need to worry about having so many memory problems in my program. It's right? If this is correct, should I not use malloc (or calloc) for every pointer I create? Just to make sure my program has fewer problems?

+4
source share
4 answers

malloc used to allocate memory. You can use the pointer either by highlighting it with malloc or by pointing to an already allocated part of the memory.

In the first example that you specified, if you did not specify a pointer to an address, it does not stand out and cannot be used. For example, you can indicate that it points to the current value of int:

 int value = 0; int* pointer; pointer = &value; 

But you cannot assign a value to it:

 int value = 0; int* pointer; *pointer = value; // wrong because pointer is not allocated 

This is your second case.

calloc is basically malloc + initialization.

Change Regardless, this is not a good example of using malloc. Best used probably when you need to allocate an array of variable size (unknown at compile time). Then you will need to use:

 int* array = (int*)malloc(N * sizeof(int)); 

This is useful for two reasons:

  • If N is a variable, you cannot perform static allocation, for example int array[N];
  • The stack may be limited by the amount of space that you can allocate.
+8
source

In the first case, you popped a pointer to an integer on the stack; and it's all. So you have one pointer.

In the second case, you indicated a pointer to an integer on the stack; and then used malloc to allocate a block of memory sufficient to hold an integer, and also made it so that your pointer to an integer points to that memory malloc (); so here you have a pointer to an integer and an integer.

+3
source

You should allocate memory (optimistically) only when necessary with calloc or malloc. A pointer can also point to an existing memory location.

0
source

The first case: you specified only 1 variable pointer. But without attributing anything to him. This is just a declaration.

But the second case: you allocate the memory pointer 1 to the heap (you can create the nth pointer on the heap), multiplying the number of the required element.

The difference between case 1 and case 2 is that in case 1, the memory will remain on the stack until the program finishes work. but in case 2 .. you can free (restore) memory at any time using the free (pointer).

0
source

Source: https://habr.com/ru/post/1385753/


All Articles