The memory model in the C language is the object storage system in the C language [1] .
The way of storing an object in the C language determines its lifetime - part of the program execution time during which the object exists or a place is reserved for it. The object has a fixed address and retains its last value. It is forbidden to refer to an object that has ceased to exist, and if a pointer was used when working with an object, its value remains undefined.
There are three ways to store objects [1] : automatic, static and dynamic .
Property | Auto | Static | Dynamic |
---|---|---|---|
Announcement | Object without binding and without static | Has internal or external binding or is declared with a static qualifier | Selected using malloc |
Time of existence | The block in which the object is declared | All program execution time | From calling malloc to calling free |
Initialization | None if there is no explicit initialization. | Occurs once before the program starts. | Partly in the case of calloc |
The size | Fixed, unchangeable | Fixed, unchangeable | Any changeable |
Typical placement | CPU stack or registers | Separate memory segment | A pile |
A static object can be initialized explicitly, or use the silent initialization.
When using the calloc
function, all objects have a zero value except floating-point numbers and pointers [2] .
Expressions that are not lvalues associated with accessing an array that is a member of a structure ( struct
) or union ( union
) have a lifetime limited by the evaluation of such an expression [1] .
C strings that initialize char*
pointers have a static storage type and should not be changed [3] .
Dynamic Memory
No object can be in dynamic memory without explicit instructions from the programmer. To work with dynamic memory there are functions malloc
, calloc
, realloc
and free
. Since functions allocating memory take a size in a size_t
variable, the maximum amount of allocated memory is limited to SIZE_T_MAX
[1] .
The malloc
and calloc
functions allocate memory, which, after use, must be freed by calling free
. After release, the pointer value remains undefined . The realloc
function returns a pointer to the changed memory block; if the request cannot be satisfied, the size of the memory block does not change [1] .
1 #include <stdlib.h>
2
3 void foo ( void ** ptr , size_t size )
4 {
5 * ptr = realloc ( * ptr , size + 128 ); / * memory leak if realloc returns NULL * /
6 if ( ! * Ptr )
7 {
8 ...
9 }
10 }
When working with dynamic memory, memory leaks and double-free block errors may occur.
Example
#include <stdlib.h>
#include <string.h>
static int x ; / * 0 by default, all runtime exists * /
static int y = 45 ; / * 45, there is all the runtime * /
int cnt ( void )
{
static int i = 0 ; / * static type, initialized by zero only at startup
programs, not every function call * /
int j = - 1 ; / * automatic type, initialized every time
when calling the function -1 * /
i ++ ; / * is incremented by 1 in the static memory region each function start * /
j ++ ; / * increment by 1 local variable * /
return ( i + j ); / * at the first call from the program launch, the function will return 1,
on the second call 2, ... * /
}
int main ( void )
{
char arr [ 50 ] = "This is an object of automatic storage duration" ;
/ * is of automatic type, exists before exiting main,
the initial 45 elements of the array are initialized with elements
lines with a closing zero, the rest are not defined * /
char * line = "Simple line" ; / * automatic type, exists before exit
from main, line initialized with pointer to constant * /
int y ; / * value undefined, exists before exiting main * /
int z = 10 ; / * value defined, exists before exiting main * /
char * ptr ; / * pointer value undefined * /
ptr = malloc ( 50 ); / * pointer value undefined
an object by pointer exists before calling free * /
strcpy ( ptr , arr );
free ( ptr );
return 0 ;
}
Notes
- 2 1 2 3 4 5 ISO / IEC 9899: 1999. 6.2.4
- ↑ ISO / IEC 9899: 1999 7.20.3
- Fa C FAQ