简体   繁体   English

初始化指针传递的数组

[英]initialize array passed by pointer

The function cannot initialize an array because sizeof() returns bytes of an int pointer not the size the memory pointed by myArray . 该函数无法初始化数组,因为sizeof()返回的是int pointer字节,而不是myArray int pointer的内存大小。

 void assignArray(int *myArray)
 {
     for(int k = 0; k < sizeof(myArray); ++k)
     {
         myArray[k] = k;
     }
 }

Are there other problems ? 还有其他问题吗?

Thanks 谢谢

Well no, there are no other problems. 好吧,没有其他问题。 The problem you stated is the only thing stopping you from initialising the array. 您指出的问题是阻止您初始化数组的唯一原因。

Typically, this is solved by simply passing the size along with the pointer: 通常,这可以通过将大小和指针一起传递来解决:

void assignArray(int* myArray, std::size_t mySize)
{
    for (std::size_t k = 0; k < mySize; ++k)
        myArray[k] = k;
}

Note that I've used std::size_t for the size because that is the standard type for storing sizes (it will be 8 bytes of 64-bit machines, whereas int usually isn't). 请注意,我使用std::size_t作为大小,因为这是存储大小的标准类型(它将是8字节的64位计算机,而int通常不是)。

In some cases, if the size is known statically, then you can use a template: 在某些情况下,如果大小是静态已知的,则可以使用模板:

template <std::size_t Size>
void assignArray(int (&myArray)[Size])
{
    for (std::size_t k = 0; k < Size; ++k)
        myArray[k] = k;
}

However, this only works with arrays, not pointers to allocated arrays. 但是,这仅适用于数组,而不适用于分配的数组的指针。

int array1[1000];
int* array2 = new int[1000];
assignArray(array1); // works
assignArray(array2); // error

I don't see other problems. 我没有看到其他问题。 However, you probably wanted this: 但是,您可能想要这样:

template<int sz>
void assignArray(int (&myArray)[sz])
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

Unless, of course, even the compiler doens't know how big it is at compile time. 当然,除非编译器都不知道在编译时的大小。 In which case you have to pass a size explicitly. 在这种情况下,您必须显式传递大小。

void assignArray(int* myArray, size_t sz)
{
    for(int k = 0; k < sz; ++k)
    {
        myArray[k] = k;
    }
}

If you don't know the size, you have a design error. 如果不知道尺寸,则表示设计错误。

http://codepad.org/Sj2D6uWz http://codepad.org/Sj2D6uWz

There are two types of arrays you should be able to distinguish. 您应该能够区分两种类型的数组。 One looks like this: 一个看起来像这样:

type name[count];

This array is of type type[count] which is a different type for each count . 此数组的类型为type[count] ,每个count类型都不同。 Although it is convertable to type * , it is different. 尽管它可以转换为type * ,但是有所不同。 One difference is that sizeof(name) gives you count*sizeof(type) 一个区别是sizeof(name)给您count*sizeof(type)

The other type of array looks like this: 另一类型的数组如下所示:

type *name;

Which is basically just a pointer that you could initialize with an array for example with malloc or new . 基本上,这只是一个指针,您可以使用数组初始化,例如,使用mallocnew The type of this variable is type * and as you can see, there are no count informations in the type. 该变量的type * ,并且您可以看到,该类型中没有计数信息。 Therefore, sizeof(name) gives you the size of a pointer in your computer, for example 4 or 8 bytes. 因此, sizeof(name)为您提供计算机中指针的大小,例如4或8个字节。

Why are these two sizeof s different, you ask? 您问这两个sizeof为什么不同? Because sizeof is evaluated at compile time. 因为sizeof是在编译时评估的。 Consider the following code: 考虑以下代码:

int n;
cin >> n;
type *name = new type[n];

Now, when you say sizeof(name) , the compiler can't know the possible future value of n . 现在,当您说sizeof(name) ,编译器无法知道n的可能将来值。 Therefore, it can't compute sizeof(name) as the real size of the array. 因此,它不能将sizeof(name)计算为数组的实际大小。 Besides, the name pointer might not even point to an array! 此外, name指针甚至可能不指向数组!

What should you do, you ask? 你问怎么办? Simple. 简单。 Keep the size of the array in a variable and drag it around where ever you take the array. 将数组的大小保留在变量中,然后将其拖动到放置数组的位置。 So in your case it would be like this: 因此,在您的情况下,它将是这样的:

void assignArray(int *myArray, int size)
{
    for(int k = 0; k < size; ++k)
    {
        myArray[k] = k;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM