1. C程序的存储空间布局:
1 #include <stdio.h>
2
3 int main(void)
4 {
5 return 0;
6 }
size Base
text data bss dec hex filename
1115 552 8 1675 68b Base
1 #include <stdio.h>
2 int arr[1000] = {1};
3 int main(void)
4 {
7 return 0;
8 }
size Drive1
text
data
bss
dec
hex filename
1115
4568
8
5691
163b
Derive1
1 #include <stdio.h>
2
3 int arr[1000];
4
5 int main(void)
6 {
7 return 0;
8 }
text data bss dec hex filename
1115 552 4032 5699 1643 Derive1
2. 存储器的分配:
NAME
malloc, free, calloc, realloc - allocate and free dynamic memory
SYNOPSIS
#include <stdlib.h>
void *malloc(size_t size);
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initial‐
ized(malloc分配出来的空间并未被初始化). If size is 0, then malloc() returns either NULL, or a unique pointer value that can later
be successfully passed to free().
The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to mal‐
loc(), calloc() or realloc().(free函数只能释放动态分配的空间)
Otherwise, or if free(ptr) has already been called before, undefined
behavior occurs. If ptr is NULL, no operation is performed.
The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the
allocated memory. The memory is set to zero.(callo函数分配出来的空间会被初始化)
If nmemb or size is 0, then calloc() returns either
NULL, or a unique pointer value that can later be successfully passed to free().
The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is
larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to
malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to
free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If
the area pointed to was moved, a free(ptr) is done.