可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Possible Duplicate:
Is array name a pointer in C?
C++ Static array vs. Dynamic array?
I'm learning C and I'm confused at what the difference is between the following two arrays:
int a[10];
and
int *b = (int *) malloc(10 * sizeof(int));
Just on the most basic level, what is the difference between these two?
回答1:
int a[10];
is allocated on stack and is de-allocated as soon as the scope ends.
int *b = (int *) malloc(10 * sizeof(int));
is allocated on heap and is alive throughout the lifetime of the program unless it's explicitly freed.
回答2:
The static array will be destroyed as soon as you leave the current stack frame (basically, when the function you're in returns). The dynamic array sticks around forever until you free() it.
回答3:
The first lives on the stack (= lives as long as the scope of the variable), the second lives on the heap (= lives until freed). The first has a fixed size, whereas the second can be re-sized.