传递给C ++函数的数组给出不同的长度 [英] Arrays passed to function in C++ giving different lengths
问题描述
我首先要说-我对幕后"所发生的事情很感兴趣,这会导致这个问题,因为我盲目地修改了代码.也许是C ++标准或我不熟悉的东西决定了导致它的原因:-)
I want to say first - I'm as interested in what's happening "behind the scenes" to cause this problem as I am in a blind fix for the code. Perhaps the C++ standard or something I'm unfamiliar with dictates what's causing it :-)
无论如何...
我试图将3个数组A, B, C
传递给一个函数,该函数将合并A
和B
,并将结果存储在C
中.
I am trying to pass 3 arrays, A, B, C
into a function, which will merge A
and B
, storing the result in C
.
函数声明:void merge(int* a, int* b, int* c)
从main()
传递给:
int A[] = {1,2,3,4};
int B[] = {6,7,8,9};
int* C; //this could be wrong, btw!
merge(A, B, C);
问题1.
奇怪的是,在main()
中,如果我打印sizeof(A)/sizeof(A[0])
的结果,它将为数组的长度"-4
-和B提供相同的结果.数组的功能,我尝试通过相同的方法 再次计算大小,但是我得到两个数组的结果2
. merge()
的第一行:
Issue 1.
What's strange is that in main()
, if I print the result of sizeof(A)/sizeof(A[0])
, it gives me the proper result for the "length" of the array - 4
- and the same thing for B. But when I pass the arrays to the function, I try to calculate the size again, by the same method, but I get the result 2
for both arrays. The first lines of merge()
:
void merge(int* a, int* b, int* c)
{
int sizeA = sizeof(a)/sizeof(a[0]);
int sizeB = sizeof(b)/sizeof(b[0]);
int totalsize = sizeA + sizeB;
std::cout << "size A = " << sizeA << std::endl; //prints 2
std::cout << "size B = " << sizeB << std::endl; //prints 2
std::cout << "total = " << totalsize << std::endl;
...
问题2.
只是为了好玩,我尝试遍历传递给merge()
的a
和b
(在合并功能中):
Issue 2.
Just for fun, I tried iterating through a
and b
passed to merge()
(within the merge function):
for (int i = 0; i < 4; ++i)
std::cout << a[i]; //prints "1234" as expected
非常酷.但是当我将索引限制增加到8 ...
All cool. But when I increase the index limit to 8...
for (int i = 0; i < 8; ++i)
std::cout << a[i]; //prints "12346789" - A and B concatenated!
多次提高最大索引,因为为什么不这样做
Raising the max index a couple more times because why not:
for (int i = 0; i < 10; ++i)
std::cout << a[i]; //prints "1234678900"
...
for (int i = 0; i < 11; ++i)
std::cout << a[i]; //prints "1234678900-444896443"
我想是由于越界索引和访问其他内存而导致的未定义行为.
Undefined behavior from out of bounds indexing and accessing other memory, I guess.
以相同的方式打印b
的操作类似:
Printing b
in the same manner does similar:
- 循环到
i = 4
将打印数组-6789
- 向
6
添加两个零-678900
- 到
8
添加了其他内容-678900-126926969126613
- looping to
i = 4
prints the array -6789
- to
6
adds two zeroes -678900
- to
8
adds the other stuff -678900-126926969126613
当然,打印C不会产生任何结果.
Printing C, of course, results in nothing.
这些奇怪之处是
- 我正在使用 C ++ Shell (带有选项
-std=c++14 -Wpedantic -O2
)的事实吗? - 将数组错误地传递给
merge()
吗? -
main()
中的不正确的初始化? - 需要具有终止符的数组,例如char数组吗?
- 以上所有?
- the fact I'm using C++ Shell (with options
-std=c++14 -Wpedantic -O2
) ? - incorrect passing of arrays to
merge()
? - incorrect initializations in
main()
? - the arrays needing to have a terminator, like char arrays?
- all of the above?
推荐答案
- 在第一种情况下,您有一个数组与指针不同.因此
sizeof
是正确计算的.而int*
是指针,而传递给函数的数组总是衰减为指针(按引用传递时除外).同样,sizeof(int*)/sizeof(int)
是计算机上指针的大小除以int
的大小,因此,如果系统是64位(8字节),并且int
的典型大小是4
,则您得到8/4=2
. - 通过超出范围索引来进行典型的未定义行为.
- In the first case, you have an array, which is not the same as a pointer. So the
sizeof
is correctly computed. Whereasint*
is a pointer, and an array passed to a function always decays to a pointer (except when passing by reference). Alsosizeof(int*)/sizeof(int)
is the size of the pointer on your machine divided by the size of theint
, so if your system is a 64 bit (8 bytes) and theint
has a typical size of4
, you get8/4=2
. - Typical undefined behaviour by indexing out of bounds.
建议:使用std::vector<int>
代替,您将避免为C
和sizeof
分配内存.您可以简单地使用std::vector::size()
成员函数来获取向量的大小.
Advice: use std::vector<int>
instead and you'll avoid allocating memory for C
and using sizeof
. You can simply use the std::vector::size()
member function to obtain the size of the vector.
这篇关于传递给C ++函数的数组给出不同的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!