(1) C Language and memory allocation method
<1> allocation from a static storage area.
Memory is allocated at the time of program compilation, and the entire running period of the program is present. For example, global variables, static variables, and so on.
<2> creating on the stack
When executing a function, the storage units of local variables within the function can be created on the stack and are freed automatically at the end of the function execution. Stack memory allocation operations are built into the processor's instruction set and are highly efficient, but the allocated memory capacity is limited.
<3> allocated from the heap, also known as dynamic memory allocation.
When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by the user, and the use is very flexible, but the problem is the most.
(2) C Language and memory application related functions are mainly alloca, calloc, malloc, free, realloc and so on.
<1>alloca is to request memory from the stack, so there is no need to release.
<2>malloc allocated memory is located in the heap, and there is no initialization of the contents of the memory, so basically malloc, after the call function Memset to initialize this part of the memory space.
<3>calloc will initialize this part of the memory, set to 0.
<4>realloc changes the size of the memory requested by malloc.
The memory that is applied to <5> is eventually freed by the function free.
When the program is in the process of malloc, but no free, it will cause a memory leak. Part of the memory is not used, but because there is no free, so the system thinks that this part of memory is still in use, resulting in continuous application of memory to the system, Reduces the amount of memory available to the system. But a memory leak simply means that when the program is running, the OS will reclaim all of the resources when the program exits. Therefore, the proper re-start of the program, sometimes a bit of a function.
"Attention"
The declarations of the three functions are:
void* malloc (unsigned size);
void* realloc (void* ptr, unsigned newsize);
void* calloc (size_t numelements, size_t sizeofelement);
Are all within the Stdlib.h library, and their return values are the addresses that are requested by the system and return NULL if the request fails.
(1) function malloc ()
Allocates a contiguous region of size bytes in the memory's dynamic store, with the parameter size being the length of the memory space to return the first address of the region.
(2) function calloc ()
Similar to malloc, the parameter sizeofelement is the unit element length of the requisition address, numelements is the number of elements, that is, a contiguous address space that requests numelements*sizeofelement byte size in memory.
(3) function realloc ()
Reassign space to a pointer that has been assigned an address, PTR is the original space address, and NewSize is the re-requested address length.
Difference:
(1) The function malloc cannot initialize the allocated memory space, and the function calloc can. If the memory space allocated by the malloc () function is not used, then each of these bits may be 0; Conversely, if this part of the memory has been allocated, There may be a variety of data left behind. That is, a program that uses the malloc () function starts (the memory space is not reassigned) to work properly, but after a while (memory space has been reassigned), problems may occur.
(2) the function calloc () initializes each bit in the allocated memory space to zero, that is, if you are allocating memory for an element of a character type or integer type, then those elements will be guaranteed to be initialized to 0, and if you are allocating memory for an element of pointer type, These elements are usually initialized to null pointers, and if you allocate memory for real data, those elements are initialized to 0 of the floating-point type.
(3) The function malloc allocates the memory space of the specified size byte to the system request. The return type is a void* type. void* represents a pointer to an indeterminate type. c,c++ Specifies that the void* type can be cast to any other type of pointer.
(4) ReAlloc can expand or shrink the space indicated by a given pointer, whether it is expanding or shrinking, The contents of the original memory will remain unchanged. Of course, for shrinking, the content of the part that is shrunk is lost. ReAlloc does not guarantee that the adjusted memory space and the original memory space remain the same memory address. Instead, the pointer returned by ReAlloc is likely to point to a new address.
(5) ReAlloc is the memory allocated from the heap. When a chunk of memory is enlarged, realloc () tries to get additional bytes directly from the bytes that exist behind the heap, and if it satisfies, it will be natural; if the bytes behind the data are not enough, the problem comes out, Then using the first free block on the heap that has enough size, the existing data is then copied to the new location, and the old block is put back on the heap. An important message of this sentence is that the data may be moved.
Example:
#include <stdio.h>
#include <malloc.h>
int main (int argc, char* argv[])
{
Char *p,*q;
p = (char *) malloc (10);
Q = p;
p = (char *) realloc (p,10);
printf ("p=0x%x/n", p);
printf ("q=0x%x/n", Q);
return 0;
}
Output: The memory address is unchanged after realloc
P=0x431a70
Q=0x431a70
#include <stdio.h>
#include <malloc.h>
int main (int argc, char* argv[])
{
Char *p,*q;
p = (char *) malloc (10);
Q = p;
p = (char *) realloc (p,1000);
printf ("p=0x%x/n", p);
printf ("q=0x%x/n", Q);
return 0;
}
Output: Memory address changed after ReAlloc
P=0x351c0
Q=0x431a70
The relation and difference between malloc realloc and calloc in C language