Hello MPGH community,
today we will talk about memory management, referring to the standard of C 99.
Let's start to say that C and C++ don't have the garbage collector, this final is present on .NET or on Java and it allows to delete items that are not needed on the heap.
Memory management in C/C++ is particularly important and forgetting to free the space may cause unexpected errors and unacceptable situation in terms of consumption ... There are two types of allocation: static and dynamic

Static allocation
The static allocation allows you to save on Data Section (a memory area that is part of the program) ...
In the Data Section there are allocated static variables, arrays and global variables.
The Stack instead, is used for local variables, or in the case it were executed a pass by value.
In fact, the programmers won't have to free the memory manually, but it will happen automatically.
We start to analyze the advantages of the stack.
- Deallocate automatically
- The access to resources in the stack is faster, as it is directed.
- Memory not fragmented

#include <stdio.h>

void stampa(int dato){ // The data as it is passed by value, in fact a copy is saved
int v=dato++; // Allocated on the stack at the time of the call, eliminated once off function
static int v2=dato; // Allocated on the stack when the call will remain in memory until its closure
printf("%i \n",v);


}

char pubb; // Allocated on the stack

int main(int arg,char *argv[]){
int a=5; // Allocated on the stack when you enter the main, which is the entry point
stampa(a); // Allocated the required variables, out the function will no longer exist
return 0;
}


The fact that free memory is automatically good, but we assume that they need to clean up the space on demand or to change the size of a given (eg a carrier), we can not do, in fact, to use the stack, we need to know before space necessary.
And, in case of frequent allocation on the stack (for example, during the recursion) it will be easy to see an error due to the lack of space (overflow). See:

Code:
...
char c[50]; // Allocated on the stack an array of type char with 50 elements during all the program there will only be 50 units
Summarizing the "disadvantages" of the stack are the followed:

- The Stack, you can use it when you know the exact size of the resource, while running it is not possible to increase/decrease the space pre-allocated
- For The stack management there are not pointers
- In General stack as assigned by the system has its limits, and of course while running it does NOT increase


Dynamic allocation
As the word itself suggests, it allows you to allocate a space on demand ...
In C to deal with these transactions are: calloc / malloc / realloc.
Calloc and malloc are used to allocate memory ... realloc instead to reallocate during the execution.
As mentioned before, when using the heap memory management it is manual in fact to free the space it is needed to call free.
But before you see some example code we analyze the advantages and disadvantages of dynamic allocation. We begin to list the advantages:
The space to be used can change at runtime, also it is requested directly and it has no limits of allocations (except when there is no space on the heap) ...
Better control on the memory:
Code:
#include <stdio.h>
#inlcude <stdlib.h>

int main(void){
int d;
int *arg=(int *)calloc(10,sizeof(int)); // void *calloc(size_t tot, size_t dimensione);
printf("reallocate memory \n");
scanf("%i",&d);
arg=(int *)realloc(arg,20); //void *realloc(void *ptr,size_t newdime)
printf("i free memory \n");
free(arg) // void free(void *ptr)
arg=Null;
return 0;
}
Before continuing the explanation of the code, we see the difference between malloc and calloc is often neglected. In fact, malloc addition to requiring a single argument that is the number of bytes required, it presents a significant detail: that the area allocated memory is not used, instead using calloc the area of allocated memory it is automatically reset. And so we see a redundant instructions when using calloc ...
Shown below:
Code:
...
arg=(int *)calloc(10,sizeof(int));
*(arg+0)=0; // Useless instruction, just in case we used malloc it was useful
The functioning of realloc is identical to calloc, it only accepts the allocated pointer and the new dimension by returning the common void pointer converted with the cast.
Let's move to free, because even here there are details that are often totally neglected ... After freeing the memory, reusing the same pointer can lead to unpredictable results and it is not immediately analyzable, which is why it tends to eliminate the location of the pointer. The solutions generally is one:

- Assign null to the pointer

In this was the location of the pointer is lost and avoids annoying anomalies or crash.
To finish this guide we list the "disadvantages" of dynamic.
Who come to this point should be understood. In any case, here's the list:

The use of over-allocation and deallocations lead to memory fragmentation
Access is slower than the stack

Well, I hope this guide has clarified the concept of static and dynamic allocation. I also add only two details:
The number of allocations is not really unlimited, in fact the constant SIZE_MAX indicates the limit of size_t (Mathematically represented by 2^8*sizeof (size_t)).
-If the heap does not have enough space, malloc or calloc return a null pointer, it is good practice to check the result of calls to malloc and calloc