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
4
  • @ArunMu That has little relation to the question. Commented May 24, 2014 at 15:15
  • 1
    For first, int* dynArray = new int[5] is not the same type as int stackArray[5]. Commented May 24, 2014 at 15:15
  • 1
    @40two Correct, that's why I am asking this question. Commented May 24, 2014 at 15:16
  • 1
    using gcc you can compile the code using the -S flag to generate the assembly code and verify by yourself how the two arrays are allocated. I would expect the dynArray to be in the heap though. Don't know if there is a similar option for VS2013 Commented May 24, 2014 at 15:20

2 Answers 2

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.)

Sign up to request clarification or add additional context in comments.

3 Comments

It may be conceptually helpful to the OP to talk about array decay as well.
stackArray[index], that expression denotes the corresponding array element: Ok, but how does that happen? Does it magically compute ??? + index? ??? should be something.
@BabkenVardanyan: Does it matter? When you have pair<int, int> 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...
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.

4 Comments

@juanchopanza Thanks. Removed the misleading part.
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?
@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 stackoverflow.com/q/21972465/1809377

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.