c中数组的声明
可能的重复:
堆栈变量与堆变量
将数组声明为:
int arr[100];
和
int* arr=(int*)malloc(sizeof(int)*100);
Which 之间有什么区别一个是首选?这涉及到堆内存和堆栈内存之类的东西吗?
Possible Duplicate:
Stack variables vs. Heap variables
What is the difference between declaring an array as:
int arr[100];
and
int* arr=(int*)malloc(sizeof(int)*100);
Which one is preferred? Is there something like heap and stack memory involved in this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议去书店买一本 Kernighan 和 Ritchie 的《C 编程语言》,也可以选择买一本 Harbison & C 语言。 Steele 的C:参考手册。
第一种情况给你一个 100 个整数的数组,分配在堆栈上。后一种情况为您提供一个指向 int 的指针,其地址是在堆上分配的缓冲区的地址,该缓冲区的大小足以包含 100 个 int。
C 语言从根本上来说是一种与硬件无关的汇编语言。指针和数组之间的区别是故意模糊的,因为数组引用符号是指针算术的语法糖。这:
与此外
,给定函数 foo() 时,编译器将(或应该)抱怨类型不匹配:
调用:
应该导致编译器抱怨类型不匹配。
I suggest a trip to the bookstore to pickup a copy of Kernighan and Ritchie's The C Programming Language, and optionally, a copy of Harbison & Steele's C: A Reference Manual.
The first case gives you an array of 100 ints, allocated on the stack. The latter case gives you a pointer to an int, whose address is that of a buffer allocated on the heap, the size of which is large enough to contain 100 ints.
The C language is fundamentally a hardware agnostic assembly language. The distinction between a pointer and an array is intentionally fuzzy, since array reference notation is syntactic sugar for pointer arithmetic. This:
is identical to
Further, the compiler will (or should) whine about type mismatches, given function
foo()
:The invocation:
should results in a compiler whine regarding type mismatches.
第一个将
arr
声明为驻留在堆栈上的int[100]
类型的数组。在某些情况下,arr
将衰减为int *
类型的指针,但这并不意味着它是一个指针。为了进一步强调它不是指针,sizeof(arr)
将产生sizeof(int) * 100
,而不是地址的大小。第二个声明一个
int *
类型的指针,并将其初始化为malloc
返回的指针,这实际上是在堆上分配内存。请注意,在这种情况下,arr
不是一个数组 - 您可以使用数组表示法来偏移和取消引用指针,但这并不会使它成为一个数组。The first one declares
arr
as an array of typeint[100]
that resides on the stack. In some contextsarr
will decay to a pointer of typeint *
, but that doesn't make it a pointer. To further emphasize that it is not a pointer,sizeof(arr)
will yieldsizeof(int) * 100
, not the size of an address.The second one declares a pointer of type
int *
and initializes it to the pointer returned bymalloc
, which in fact allocates memory on the heap. Note that in this casearr
is not an array - you can use the array notation to offset and dereference the pointer, but that doesn't make it an array.前者自动分配一个大小为 100 的数组,一旦超出范围就会自动释放。
后者在空闲存储中分配 100 int 的空间,并返回指向内存块开头的指针。它将保持分配状态,直到在指针上调用
free()
。如果您只需要与当前作用域一样长的内存,那么前者更容易使用(假设它不是太大)。否则,可以采用后一个选项。
The former allocates an automatic array of size 100, which will be automatically released once it goes out of scope.
The latter allocates space for 100
int
s in the free store, and returns a pointer to the start of the block of memory. It will remain allocated untilfree()
is called on the pointer.If you need the memory only for as long as the current scope, then the former is easier to work with (assuming it's not too big.) Otherwise, the latter option is the approach to take.