C dynamic memory allocationIn the C programming language, many of the commands in a program change the memory of the computer running that program. The memory comes in different ways, with some memory always made available to every part program, some memory given over to the functions and code blocks in the program, and some memory which can be given to the program as it needs it, and freed up when the need has finished. This memory that is allocated to the program and freed later is known as dynamic memory. (The other types are known as static memory and automatic memory.) The dynamic memory allocation refers to allocating memory during a program's run time. Dynamically allocated memory is obtained from a storage pool called a heap. A group of functions in the C standard library are typically used for dynamic memory allocation. Overview of functionsC dynamic memory allocation functions are defined in stdlib.h header.
Differences between malloc and calloc
Bugs and security implicationsThe C language does not by default check that the calls to these functions, or the accesses to the arrays they create, are used correctly. If the program does something invalid, such as accessing memory beyond what has been allocated, or using memory that has been freed, this can cause the program to crash or to behave in some other undesirable way. In some cases, improper use of these functions can create security vulnerabilities that allow hackers to read or modify sensitive data or run malicious code. Example code#include <stdlib.h>
int main() {
int *array = malloc(10 * sizeof(int));
free(array);
}
|
Portal di Ensiklopedia Dunia