Why does C ++ have an array of types?

I am learning C ++. I found that the index has the same function with the array, which a[4]

, a

may be a pointer or array. But since C ++ is defined, for different array lengths, this is a different type. In fact, when we pass an array to a function, it will be automatically converted to a pointer, I think this is further proof that the array can be replaced with a pointer. So my question is:

Why doesn't C ++ replace the entire array with a pointer?

+3


source to share


5 answers


In the beginning of C, it was decided to represent the size of an array as part of its operator-accessible type sizeof

. C ++ should be backward compatible with this. There's a lot wrong with C ++ arrays, but size as part of that type isn't one of the wrong things.


Relatively

" Pointer has the same function with the array, which a[4]

, a

can be both a pointer and array

no, it's just an implicit conversion, from an array expression to a pointer to the first element of that array.

Oddly enough, C ++ doesn't provide indexing for built-in arrays. There's indexing for pointers and p[i]

just means *(p+i)

by definition, so you can also write this as *(i+p)

and therefore as i[p]

. And in this way also i[a]

, because it really is a pointer that is indexed. It's strange.

An implicit conversion called "fading" loses information and is one of the things that is wrong about C ++ arrays.



Indexing of pointers is the second thing that is wrong (even if it makes a lot of sense for assembly language and machine code level).

But it must remain that way for backward compatibility.


Why does Bad β„’ array decay: This causes the array to T

often be represented by a simple pointer to T

.

You cannot see from such a pointer (as a formal argument, for example) whether it points to a single object T

or to the first element of an array T

.

But it is much worse if it T

has a derived class TD

where sizeof(TD) > sizeof(T)

, and you form an array from TD

, then you can pass this array to a formal argument by pointing to T

- because this array TD

decays to a pointer to TD

, which is implicitly converted to a pointer to T

. Now, using this pointer to T

as an array results in incorrect address computation due to incorrect size assumptions for array elements. And hit crash (if you're lucky), or maybe just wrong results (if you're unlucky).

+8


source


In C and C ++, all of the same type are of the same size. An array int[4]

is twice as large as an array int[2]

, so they cannot be of the same type.

But then you might ask, "Why enter a size?" Well:



  • The local variable must occupy a certain amount of memory. When you declare an array, it takes up memory that scales with its length. When you declare a pointer, it is always the size of the pointers on your computer.
  • Pointer arithmetic is determined by the size of the type it points to: the distance between the address it points p

    to and the pointer to p+1

    is exactly the size of its type. If the types do not have fixed sizes, then p

    additional information will need to be carried, or C should abandon arithmetic.
  • The function needs to know how big its arguments are, because the functions are compiled to expect their variables to be in specific locations, and have a parameter with unknown screw sizes.

    And you say, "Well, if I pass an array to a function, it still turns into a pointer." True, but you can create new types that have arrays as members, and then you can pass THOSE types. And in C ++, you can pass an array as an array.

    int sum10(int (&arr)[10]){ //only takes int arrays of size 10
        int result = 0;
        for(int i=0; i<10; i++)
            result += arr[i];
        return result
    }
    
          

+1


source


You cannot use pointers instead of array declarations without using malloc / free or new / delete to create and destroy memory on the heap. You can declare an array as a variable and create it on the stack, and you don't need to worry about it.

0


source


Well, an array is easier to deal with and manipulate with data. However, in order to use pointers, you need to have a clear memory address to point to. Also, both concepts are no different when it comes to handing over their function. Bothe pointers and arrays are passed by reference. Hope it helps

0


source


I'm not sure if I get your question, but I assume you are new to coding:

when you declare an array int a [4], you are letting the compiler know that you need 4 * int memory, and what the compiler does is assign the "start" address of that 4-digit memory size. when u later uses [x], [x] means to do (a + sizeof (int) * x) AND look up that pointer address to get an int.

In other words, it always skips a pointer instead of an "array" which is just an abstraction that makes it easier for you to code.

0


source







All Articles