These of 2 of the probably many ways of declaring arrays (and allocating memory for them) in c++
这些可能是在c ++中声明数组(并为它们分配内存)的两种方法中的两种
1. int a[3];
2. int *b = new int[3];
I want to understand how c++ is treating the two differently.
我想了解c ++如何以不同的方式处理这两者。
a. In both cases, i can access array with the following syntax: a[1]
and b[1]
一个。在这两种情况下,我都可以使用以下语法访问数组:a [1]和b [1]
b. When i try cout<< a
and cout<< b
, both print the addresses of first element of respective arrays.
湾当我尝试cout << a和cout << b时,两者都打印各个数组的第一个元素的地址。
It looks to me as if both a and b are being treated as pointers to first elements of arrays.
它看起来好像a和b都被视为指向数组的第一个元素的指针。
c. But strangely, when i try to do cout << sizeof(a)
and sizeof(b)
they print different values - 4 and 12 respectively.
C。但奇怪的是,当我尝试做cout << sizeof(a)和sizeof(b)时,他们分别打印出不同的值--4和12。
I don't understand why in case of sizeof(b)
, the size of entire array is being printed.
我不明白为什么在sizeof(b)的情况下,正在打印整个数组的大小。
5 个解决方案
#1
8
a
is an array (type int [3]
)b
is a pointer (type int*
)
a是数组(类型int [3])b是指针(类型int *)
In C++ they are completely different things.
在C ++中,它们是完全不同的东西。
The sizeof
an array is the number of elements times the size of each element.
The sizeof
a pointer is independent of the size of the array (usually 4 or 8 bytes).
数组的大小是元素的数量乘以每个元素的大小。指针的大小与数组的大小无关(通常为4或8个字节)。
The only thing that arrays and pointers have in common is that arrays often "decay" to pointers in several situations. That's what's happening when you print out their value.
数组和指针的唯一共同点是数组在几种情况下经常“衰减”到指针。当你打印出它们的价值时,就会发生这种情况。
#2
3
As you noted, it seems as though both a
and b
are pointers to the start of the array. But in reality, only b
is a pointer. a
is actually an array.
正如您所指出的,似乎a和b都是指向数组开头的指针。但实际上,只有b是一个指针。 a实际上是一个数组。
The difference between the two is subtle when writing (or reading) code. The variable a
is treated as a regular variable (just like an int
or double
) in that it has an automatically allocated portion of memory assigned to it. For comparison, suppose you had declared int i
. The variable i
is the name given to a certain set of contiguous bytes in memory to hold an integer value (4 bytes on your machine). Similarly, a
is the name given to the set of contiguous bytes which holds your array (12 bytes in your case).
在编写(或读取)代码时,两者之间的差异是微妙的。变量a被视为常规变量(就像int或double一样),因为它具有分配给它的自动分配的内存部分。为了比较,假设你已经声明了int i。变量i是给予内存中某组连续字节的名称,用于保存整数值(机器上的4个字节)。类似地,a是保存数组的连续字节集的名称(在您的情况下为12个字节)。
In contrast b
is only a pointer to a single memory location. In your case, there is a block of 12 bytes which is dynamically allocated (via new int[3]
). b
itself is an automatically allocated 4-byte pointer which points to the first int
value in that 12 byte block.
相反,b仅是指向单个存储器位置的指针。在您的情况下,有一个12字节的块,它是动态分配的(通过new int [3])。 b本身是一个自动分配的4字节指针,指向该12字节块中的第一个int值。
So they really are two different kinds of things. This is made less clear to C++ programmers. One reason for this is the fact that you can use the []
operator on both types. Another reason is that arrays implicitly degenerate to pointers in several situations (e.g. in the function void Foo(int a[3]);
, a
is not actually an array, but a pointer to the beginning of the array). But don't be fooled - arrays are not pointers (as many people claim), and pointers are definitely not arrays.
所以他们真的是两种不同的东西。这对C ++程序员来说不太清楚。其中一个原因是您可以在两种类型上使用[]运算符。另一个原因是数组在几种情况下隐式退化为指针(例如在函数void Foo(int a [3]);, a实际上不是数组,而是指向数组开头的指针)。但不要被愚弄 - 数组不是指针(正如许多人所声称的那样),指针绝对不是数组。
#3
0
1 is allocated on the stack. Arrays on the stack must have a size known at compile-time.
1在堆栈上分配。堆栈上的数组必须具有编译时已知的大小。
2 is allocated on the heap. Arrays on the heap have no such requirement. Remember if you allocate with new[]
you need to deallocate it later with delete[]
:
2在堆上分配。堆上的数组没有这样的要求。请记住,如果使用new []进行分配,则需要稍后使用delete []解除分配:
int* b = new int[3];
delete[] b;
#4
0
C++ arrays in the stack have a limitation that
堆栈中的C ++数组有一个限制
int array[10];
sizeof(array) needs to be compile-time constant and
sizeof(array[0])==sizeof(array[1])==...==sizeof(array[9])
C++ arrays in the heap only have the 2nd limitation, but not the first one. (this allows array size to be determined on runtime)
堆中的C ++数组只有第二个限制,但不是第一个限制。 (这允许在运行时确定数组大小)
#5
-1
Stack arrays (#1) have sizes limited by the stack. Heap arrays (#2) have sizes limited by the heap (which is bigger than the stack).
堆栈阵列(#1)的大小受堆栈限制。堆数组(#2)的大小受堆的限制(比堆栈大)。
There are some significant performance advantages to stack allocations, look up this thread for more information: Which is faster: Stack allocation or Heap allocation
堆栈分配有一些显着的性能优势,查找此线程以获取更多信息:哪个更快:堆栈分配或堆分配
#1
8
a
is an array (type int [3]
)b
is a pointer (type int*
)
a是数组(类型int [3])b是指针(类型int *)
In C++ they are completely different things.
在C ++中,它们是完全不同的东西。
The sizeof
an array is the number of elements times the size of each element.
The sizeof
a pointer is independent of the size of the array (usually 4 or 8 bytes).
数组的大小是元素的数量乘以每个元素的大小。指针的大小与数组的大小无关(通常为4或8个字节)。
The only thing that arrays and pointers have in common is that arrays often "decay" to pointers in several situations. That's what's happening when you print out their value.
数组和指针的唯一共同点是数组在几种情况下经常“衰减”到指针。当你打印出它们的价值时,就会发生这种情况。
#2
3
As you noted, it seems as though both a
and b
are pointers to the start of the array. But in reality, only b
is a pointer. a
is actually an array.
正如您所指出的,似乎a和b都是指向数组开头的指针。但实际上,只有b是一个指针。 a实际上是一个数组。
The difference between the two is subtle when writing (or reading) code. The variable a
is treated as a regular variable (just like an int
or double
) in that it has an automatically allocated portion of memory assigned to it. For comparison, suppose you had declared int i
. The variable i
is the name given to a certain set of contiguous bytes in memory to hold an integer value (4 bytes on your machine). Similarly, a
is the name given to the set of contiguous bytes which holds your array (12 bytes in your case).
在编写(或读取)代码时,两者之间的差异是微妙的。变量a被视为常规变量(就像int或double一样),因为它具有分配给它的自动分配的内存部分。为了比较,假设你已经声明了int i。变量i是给予内存中某组连续字节的名称,用于保存整数值(机器上的4个字节)。类似地,a是保存数组的连续字节集的名称(在您的情况下为12个字节)。
In contrast b
is only a pointer to a single memory location. In your case, there is a block of 12 bytes which is dynamically allocated (via new int[3]
). b
itself is an automatically allocated 4-byte pointer which points to the first int
value in that 12 byte block.
相反,b仅是指向单个存储器位置的指针。在您的情况下,有一个12字节的块,它是动态分配的(通过new int [3])。 b本身是一个自动分配的4字节指针,指向该12字节块中的第一个int值。
So they really are two different kinds of things. This is made less clear to C++ programmers. One reason for this is the fact that you can use the []
operator on both types. Another reason is that arrays implicitly degenerate to pointers in several situations (e.g. in the function void Foo(int a[3]);
, a
is not actually an array, but a pointer to the beginning of the array). But don't be fooled - arrays are not pointers (as many people claim), and pointers are definitely not arrays.
所以他们真的是两种不同的东西。这对C ++程序员来说不太清楚。其中一个原因是您可以在两种类型上使用[]运算符。另一个原因是数组在几种情况下隐式退化为指针(例如在函数void Foo(int a [3]);, a实际上不是数组,而是指向数组开头的指针)。但不要被愚弄 - 数组不是指针(正如许多人所声称的那样),指针绝对不是数组。
#3
0
1 is allocated on the stack. Arrays on the stack must have a size known at compile-time.
1在堆栈上分配。堆栈上的数组必须具有编译时已知的大小。
2 is allocated on the heap. Arrays on the heap have no such requirement. Remember if you allocate with new[]
you need to deallocate it later with delete[]
:
2在堆上分配。堆上的数组没有这样的要求。请记住,如果使用new []进行分配,则需要稍后使用delete []解除分配:
int* b = new int[3];
delete[] b;
#4
0
C++ arrays in the stack have a limitation that
堆栈中的C ++数组有一个限制
int array[10];
sizeof(array) needs to be compile-time constant and
sizeof(array[0])==sizeof(array[1])==...==sizeof(array[9])
C++ arrays in the heap only have the 2nd limitation, but not the first one. (this allows array size to be determined on runtime)
堆中的C ++数组只有第二个限制,但不是第一个限制。 (这允许在运行时确定数组大小)
#5
-1
Stack arrays (#1) have sizes limited by the stack. Heap arrays (#2) have sizes limited by the heap (which is bigger than the stack).
堆栈阵列(#1)的大小受堆栈限制。堆数组(#2)的大小受堆的限制(比堆栈大)。
There are some significant performance advantages to stack allocations, look up this thread for more information: Which is faster: Stack allocation or Heap allocation
堆栈分配有一些显着的性能优势,查找此线程以获取更多信息:哪个更快:堆栈分配或堆分配