Dynamic memory: malloc, free, and realloc · 动态内存:malloc、free 与 realloc
Stack memory vs heap memory
- Normal local variables live on the stack. They appear when a function starts and vanish when it returns.
- Sometimes you need memory that outlives the function, or whose size you only know at run time.
- That memory comes from the heap. You ask for it, and later you give it back.
栈内存与堆内存
- 普通的局部变量住在栈上。它们在函数开始时出现,在函数返回时消失。
- 有时你需要的内存要活得比函数久,或者它的大小只有在运行时才知道。
- 这种内存来自堆。你向它要,之后再还回去。
malloc and sizeof
mallocasks the heap for a block of bytes and returns a pointer to it.- Use
sizeofto get the size of one item:malloc(n * sizeof(int))makes room fornints. - The block is yours to use through the pointer, exactly like an array:
a[0],a[1], ...
malloc 与 sizeof
malloc向堆要一块字节,并返回指向它的指针。- 用
sizeof取得一个元素的大小:malloc(n * sizeof(int))为n个 int 腾出空间。 - 这块内存归你用,通过指针访问,就和数组一样:
a[0]、a[1]……
free and memory leaks
- When you are done with heap memory, give it back with
free(pointer). - If you never
freeit, the program leaks memory — it holds space it no longer uses. - Rule for these tasks: your function allocates and returns; the checker frees it. Do not call
freeyourself.
free 与内存泄漏
- 用完堆内存后,用
free(指针)把它还回去。 - 如果你永远不
free,程序就会泄漏内存 —— 它占着不再使用的空间。 - 这些任务的规则:**你的函数负责分配并返回;检查器负责 free。**不要自己调用
free。
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *a = malloc(3 * sizeof(int)); // room for 3 ints
a[0] = 5; a[1] = 6; a[2] = 7;
printf("%d\n", a[0] + a[1] + a[2]); // 18
free(a); // give it back
return 0;
}
Growing an array with realloc
realloc(a, newSize)resizes a heap block, keeping the old contents.- It may move the block, so always use its return value:
a = realloc(a, ...). - This is how you grow an array one item at a time, like a list that gets longer.
用 realloc 扩大数组
realloc(a, newSize)给堆上的一块内存改变大小,并保留原来的内容。- 它可能会移动这块内存,所以一定要用它的返回值:
a = realloc(a, ...)。 - 这就是一次加一个元素地扩大数组的方法,就像一个会变长的列表。
Common mistakes
- Every
mallocneeds a matchingfree; do not use memory afterfree. - Check that
mallocdid not returnNULL.
常见错误
- 每个
malloc都要有对应的free;free之后不要再用那块内存。 - 检查
malloc没有返回NULL。
Now you try
- Use
malloc(n * sizeof(int))for new arrays, and#include <stdlib.h>. - Your function returns the pointer; the checker frees it — do not call
free. Do not write amain.
现在轮到你了
- 新数组用
malloc(n * sizeof(int)),并#include <stdlib.h>。 - 你的函数返回指针;检查器会 free 它 —— 你不要调用
free。也不要写main。
Complete int *make_array(int n, int value) so it allocates n ints (with malloc), sets every one to value, and returns the pointer. The checker frees it. Do not · 不 write a main. · 完成 int *make_array(int n, int value),让它用 malloc 分配 n 个 int,把每一个都设为 value,并返回指针。检查器会负责 free。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int *concat(const int a[], int na, const int b[], int nb) so it allocates na + nb ints, copies all of a then all of b, and returns the pointer. The checker frees it. Do not · 不 write a main. · 完成 int *concat(const int a[], int na, const int b[], int nb),让它分配 na + nb 个 int,先复制 a 的全部、再复制 b 的全部,然后返回指针。检查器会负责 free。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int *push(int *a, int *len, int value) so it grows the heap array a to hold one more int with realloc, stores value at the end, increases *len by 1, and returns the new pointer. The checker frees it. Do not · 不 write a main. · 完成 int *push(int *a, int *len, int value),让它用 realloc 把堆数组 a 扩大一个 int,把 value 存到末尾,把 *len 加 1,并返回新指针。检查器会负责 free。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。