c中数组的声明

发布于 2024-11-24 02:04:07 字数 348 浏览 0 评论 0原文

可能的重复:
堆栈变量与堆变量

将数组声明为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2024-12-01 02:04:07

我建议去书店买一本 Kernighan 和 Ritchie 的《C 编程语言》,也可以选择买一本 Harbison & C 语言。 Steele 的C:参考手册

第一种情况给你一个 100 个整数的数组,分配在堆栈上。后一种情况为您提供一个指向 int 的指针,其地址是在堆上分配的缓冲区的地址,该缓冲区的大小足以包含 100 个 int。

C 语言从根本上来说是一种与硬件无关的汇编语言。指针和数组之间的区别是故意模糊的,因为数组引用符号是指针算术的语法糖。这:

int foo( int a )
{
  int x[100] = load_X() ;
  int y = x[ a ] ;
  return y ;
}

与此外

int foo( int a )
{
  int *x     = load_X() ;
  int y      = *( x + a ) ;
  // note that the use of sizeof() isn't required.  For the pointer carries around
  // an implicit increment size (the size of the object to which it points). The above
  // is equivalent to
  //
  //   int y = *(int*)((char*)x + (a*sizeof(*x)) ) ;
}

,给定函数 foo() 时,编译器将(或应该)抱怨类型不匹配:

public void foo( int a[] )
{
  ...
}

调用:

int *p = malloc(...) ;

foo(p) ;

应该导致编译器抱怨类型不匹配。

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:

int foo( int a )
{
  int x[100] = load_X() ;
  int y = x[ a ] ;
  return y ;
}

is identical to

int foo( int a )
{
  int *x     = load_X() ;
  int y      = *( x + a ) ;
  // note that the use of sizeof() isn't required.  For the pointer carries around
  // an implicit increment size (the size of the object to which it points). The above
  // is equivalent to
  //
  //   int y = *(int*)((char*)x + (a*sizeof(*x)) ) ;
}

Further, the compiler will (or should) whine about type mismatches, given function foo():

public void foo( int a[] )
{
  ...
}

The invocation:

int *p = malloc(...) ;

foo(p) ;

should results in a compiler whine regarding type mismatches.

呆橘 2024-12-01 02:04:07

第一个将 arr 声明为驻留在堆栈上的 int[100] 类型的数组。在某些情况下,arr 将衰减为 int * 类型的指针,但这并不意味着它是一个指针。为了进一步强调它不是指针,sizeof(arr) 将产生 sizeof(int) * 100,而不是地址的大小。

第二个声明一个 int * 类型的指针,并将其初始化为 malloc 返回的指针,这实际上是在堆上分配内存。请注意,在这种情况下,arr 不是一个数组 - 您可以使用数组表示法来偏移和取消引用指针,但这并不会使它成为一个数组。

The first one declares arr as an array of type int[100] that resides on the stack. In some contexts arr will decay to a pointer of type int *, but that doesn't make it a pointer. To further emphasize that it is not a pointer, sizeof(arr) will yield sizeof(int) * 100, not the size of an address.

The second one declares a pointer of type int * and initializes it to the pointer returned by malloc, which in fact allocates memory on the heap. Note that in this case arr is not an array - you can use the array notation to offset and dereference the pointer, but that doesn't make it an array.

找回味觉 2024-12-01 02:04:07

前者自动分配一个大小为 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 ints in the free store, and returns a pointer to the start of the block of memory. It will remain allocated until free() 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文