传递给C ++函数的数组给出不同的长度 [英] Arrays passed to function in C++ giving different lengths

查看:92
本文介绍了传递给C ++函数的数组给出不同的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先要说-我对幕后"所发生的事情很感兴趣,这会导致这个问题,因为我盲目地修改了代码.也许是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传递给一个函数,该函数将合并AB,并将结果存储在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()ab(在合并功能中):


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?

推荐答案

  1. 在第一种情况下,您有一个数组与指针不同.因此sizeof是正确计算的.而int*是指针,而传递给函数的数组总是衰减为指针(按引用传递时除外).同样,sizeof(int*)/sizeof(int)是计算机上指针的大小除以int的大小,因此,如果系统是64位(8字节),并且int的典型大小是4,则您得到8/4=2.
  2. 通过超出范围索引来进行典型的未定义行为.
  1. In the first case, you have an array, which is not the same as a pointer. So the sizeof is correctly computed. Whereas int* is a pointer, and an array passed to a function always decays to a pointer (except when passing by reference). Also sizeof(int*)/sizeof(int) is the size of the pointer on your machine divided by the size of the int, so if your system is a 64 bit (8 bytes) and the int has a typical size of 4, you get 8/4=2.
  2. Typical undefined behaviour by indexing out of bounds.

建议:使用std::vector<int>代替,您将避免为Csizeof分配内存.您可以简单地使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆