3

I am learning C++. I found that the pointer has the same function with array, like a[4], a can be both pointer and array. But as C++ defined, for different length of array, it is a different type. In fact when we pass an array to a function, it will be converted into pointer automatically, I think it is another proof that array can be replaced by pointer. So, my question is:

Why C++ don't replace all the array with pointer?

David G
  • 94,763
  • 41
  • 167
  • 253
maple
  • 1,828
  • 2
  • 19
  • 28
  • I'm not sure of the actual reason but one is that a pointer often points to a single element, while an array does to an array. It makes it easier for a human to read. – BWG Dec 22 '14 at 02:46
  • 1
    Try doing multi-dimensional arrays without array types. – rici Dec 22 '14 at 02:54
  • "I think it is another proof that array can be replaced by pointer": that isn't a logical inference. There is an information loss, specifically the `sizeof` information. – user207421 Dec 22 '14 at 02:56
  • Maybe because arrays are a data structure common in many languages such as FORTRAN, and BASIC which predate C. Pointers are a harder issue to understand than an array. I there are reason we can't have both? – Thomas Matthews Dec 22 '14 at 02:59
  • Re the duplicate voting, since the OP asks why C++ doesn't replace all array with pointer, the fact that arrays are not pointers is understood. It's not asked about. But that's what the alleged duplicate is about – a very different question. Thus I'm voting to reopen. – Cheers and hth. - Alf Dec 22 '14 at 03:11

5 Answers5

8

In early C it was decided to represent the size of an array as part of its type, available via the sizeof operator. C++ has to be backward compatible with that. There's much wrong with C++ arrays, but having size as part of the type is not one of the wrong things.


Regarding

pointer has the same function with array, like a[4], a can be both pointer and array

no, this is just an implicit conversion, from array expression to pointer to first item of that array.

A weird as it sounds, C++ does not provide indexing of built-in arrays. There's indexing for pointers, and p[i] just means *(p+i) by definition, so you can also write that as *(i+p) and hence as i[p]. And thus also i[a], because it's really the pointer that's indexed. Weird indeed.

The implicit conversion, called a “decay”, loses information, and is one of the things that are wrong about C++ arrays.

The indexing of pointers is a second thing that's wrong (even if it makes a lot of sense at the assembly language and machine code level).

But it needs to continue to be that way for backward compatibility.


Why array decay is Bad™: this causes an array of T to often be represented by simply a pointer to T.

You can't see from such a pointer (e.g. as a formal argument) whether it points to a single T object or to the first item of an array of T.

But much worse, if T has a derived class TD, where sizeof(TD) > sizeof(T), and you form an array of TD, then you can pass that array to a formal argument that's pointer to T – because that array of TD decays to pointer to TD which converts implicitly to pointer to T. Now using that pointer to T as an array yields incorrect address computations, due to incorrect size assumption for the array items. And bang crash (if you're lucky), or perhaps just incorrect results (if you're not so lucky).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • @anonymous downvoter: please explain what you think is wrong with this answer. we're not mind readers. your downvote isn't enough for us to understand what to correct. – Cheers and hth. - Alf Dec 22 '14 at 11:34
  • As a brief follow-on to this answer, since the question is also tagged 'C': What @Alf points out (no pun intended) about the way pointer and array indexing equivalency (i.e. `0[p] == p[0]` or `"test"[3] == 3["test"]` ) is also true in plain-old-C, in case anyone thought it might be a C++-only thing. – frasnian Dec 22 '14 at 23:59
1

In C and C++, everything of a single type has the same size. An int[4] array is twice as big as an int[2] array, so they can't be of the same type.

But then you might ask, "Why should type imply size?" Well:

  • A local variable needs to take up a certain amount of memory. When you declare an array, it takes up memory that scales up with its length. When you declare a pointer, it is always the size of pointers on your machine.
  • Pointer arithmetic is determined by the size of the type it's pointing to: the distance between the address pointed to by p and that pointed to by p+1 is exactly the size of its type. If types didn't have fixed sizes, then p would need to carry around extra information, or C would have to give up arithmetic.
  • A function needs to know how big its arguments are, because functions are compiled to expect their variables to be in particular places, and having a parameter with an unknown size screws that up.

    And you say, "Well, if I pass an array to a function, it just turns into a pointer anyway." True, but you can make new types that have arrays as members, and then you can pass THOSE types around. And in C++, you can in fact 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
    }
    
leewz
  • 3,201
  • 1
  • 18
  • 38
0

You can't use pointers in place of array declarations without having to use malloc/free or new/delete to create and destroy memory on the heap. You can declare an array as a variable and it gets created on the stack and you do not have to worry about it's destruction.

Scooter
  • 6,802
  • 8
  • 41
  • 64
0

Well, array is an easier was of dealing with data and manipulating them. However, In order to use pointers you need to have a clear memory address to point to. Also, both concepts are not different from each other when it comes to passing them to a function. Bothe pointers and arrays are passed by reference. Hope that helps

Anas
  • 359
  • 1
  • 5
  • 14
0

I'm not sure if i get your question but assuming you're new to coding:

when you declare an array int a[4] you let the compiler know you need 4*int memory, and what the compiler does is assign a the address of the 'start' of that 4*int size memory. when u later use a[x], [x] means to do (a + sizeof(int)*x) AND dereference that pointer address to get the int.

In other words, it's always a pointer being passed around instead of an 'array', which is just an abstraction that makes it easier for you to code.