0

Consider the following code:

#include <iostream>

int main()
{
    int index;
    std::cin >> index;

    int* dynArray = new int[5];
    dynArray[index] = 1;

    int stackArray[5];
    stackArray[index] = 1;
}

I know for sure that dynArray is a simple int* pointer and it takes additional sizeof(int*) bytes on stack.

Question 1: Is an additional pointer variable also created on stack for stackArray? If so, does that always happen? If not, then how does stackArray[index] = 1; work without knowing array base, i.e. how does the compiler decide what value to add to index for calculating address?

Question 2: Is there any difference between C/C++?

I want an answer for these 2 environments:

  1. GCC/Linux, x86
  2. Visual Stuidio 2013, x86
Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87

2 Answers2

6
  • dynarray is a variable. Its type is int *, and it has automatic storage duration ("on the stack"). Its value is a pointer to some element somewhere else. The fact that it happens to point to an element of some array somewhere else is to some extent incidental.

  • stackArray is a variable. Its type is int[5] and it has automatic storage duration. Its value is an array of five integers. When you use the name of the variable in an expression like stackArray[index], that expression denotes the corresponding array element (which is a subobject of the array object).

Note that in your example, the array of which dynarray[index] is an element is not itself a variable, nor is the element itself. You can only access the element objects because you have a pointer to them. (And in fact you cannot express the array object itself at all, since its type is know knowable at compile time. Dynamic arrays are the one part of C++ where you have truly dynamic typing.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • It may be conceptually helpful to the OP to talk about array decay as well. – Robert Mason May 24 '14 at 15:33
  • `stackArray[index], that expression denotes the corresponding array element`: Ok, but how does that happen? Does it magically compute `??? + index`? `???` should be **something**. – Babken Vardanyan May 24 '14 at 15:54
  • @BabkenVardanyan: Does it matter? When you have `pair x;` and you say `x.second = 10`, do you care about "how it works"? The language has a notion of array types, and of addressing the element subobjects of array objects. Isn't that enough? (Yes, there are rules about pointer arithmetic and array-to-pointer decay, but I don't think you need to know those to understand arrays.) You could similarly ask how `int a; a = 10;` works (why don't you have to say `*&a = 10`?) - but being able to access variables is just part of the language... – Kerrek SB May 24 '14 at 18:29
5
  1. stackArray is an array, not a pointer. Its type is int[5], i.e., an array of 5 integers. The compiler knows the type of elements of the array which is int. stackArray[index] is evaluated to *(stackArray + index). Here, the array stackArray evaluates to a pointer to its first element.

  2. C and C++ are same in terms of an array which has automatic storage allocation.

ajay
  • 9,402
  • 8
  • 44
  • 71
  • 1
    @juanchopanza Thanks. Removed the misleading part. – ajay May 24 '14 at 15:30
  • `Here, the array stackArray evaluates to a pointer to its first element`: Ok, then where is that evaluation result stored? Is it evaluated (computed) at runtime or compile time? – Babken Vardanyan May 24 '14 at 15:46
  • 1
    @BabkenVardanyan It is evaluated during compile time. It's not stored but used to determine the address where the value `1` is stored in the statement `stackArray[index] = 1;`. Please read this for details http://stackoverflow.com/q/21972465/1809377 – ajay May 24 '14 at 16:20
  • 1
    I'll also leave this here http://en.wikipedia.org/wiki/Addressing_mode#Base_plus_index – Babken Vardanyan May 24 '14 at 17:26