Is a pointer variable created for stack local array?

Babken Vardanyan

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When local variable stack gets created?

From Dev

When local variable stack gets created?

From Dev

declaring a pointer or a stack variable

From Dev

declaring a pointer or a stack variable

From Dev

Pointer to a variable stored in the stack

From Dev

Can you define an array on the stack and pass the pointer to a global variable?

From Dev

What part of an array local variable is stored on the stack? And which part is on the heap?

From Dev

Why local variable pointer?

From Dev

Should a pointer to stack variable be volatile?

From Dev

Local variable position on stack not changing

From Dev

Return a pointer that points to a local variable

From Dev

Pointer is assigned to wrong local variable

From Dev

Casting a (pointer to a variable sized array) to (pointer to pointer)

From Dev

local array variable is not an array

From Dev

Variable sized pointer array

From Dev

fill an array in C: is it better to create a static local variable or a passing an array by pointer

From Dev

Splint unable to check maxSet on pointer to stack variable

From Dev

Pointer variable inside a function points to stack or heap?

From Dev

Splint unable to check maxSet on pointer to stack variable

From Dev

Stack around the variable was corrupted with pointer arithmetics

From Dev

Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

From Dev

Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

From Dev

copying a pointer created dynamically in array of pointers

From Java

Why can't I access a pointer to pointer for a stack array?

From Dev

Confusion between array and pointer variable

From Dev

Returning a pointer of a local variable C++

From Dev

Passing pointer to local variable to function: is it safe?

From Dev

returning static pointer to local variable from function

From Dev

Is it safe to pass pointer of a local variable to a channel in Golang?

Related Related

  1. 1

    When local variable stack gets created?

  2. 2

    When local variable stack gets created?

  3. 3

    declaring a pointer or a stack variable

  4. 4

    declaring a pointer or a stack variable

  5. 5

    Pointer to a variable stored in the stack

  6. 6

    Can you define an array on the stack and pass the pointer to a global variable?

  7. 7

    What part of an array local variable is stored on the stack? And which part is on the heap?

  8. 8

    Why local variable pointer?

  9. 9

    Should a pointer to stack variable be volatile?

  10. 10

    Local variable position on stack not changing

  11. 11

    Return a pointer that points to a local variable

  12. 12

    Pointer is assigned to wrong local variable

  13. 13

    Casting a (pointer to a variable sized array) to (pointer to pointer)

  14. 14

    local array variable is not an array

  15. 15

    Variable sized pointer array

  16. 16

    fill an array in C: is it better to create a static local variable or a passing an array by pointer

  17. 17

    Splint unable to check maxSet on pointer to stack variable

  18. 18

    Pointer variable inside a function points to stack or heap?

  19. 19

    Splint unable to check maxSet on pointer to stack variable

  20. 20

    Stack around the variable was corrupted with pointer arithmetics

  21. 21

    Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

  22. 22

    Why each slot of the local variable array in a stack frame is of 4 bytes, and not 1 byte in the JVM?

  23. 23

    copying a pointer created dynamically in array of pointers

  24. 24

    Why can't I access a pointer to pointer for a stack array?

  25. 25

    Confusion between array and pointer variable

  26. 26

    Returning a pointer of a local variable C++

  27. 27

    Passing pointer to local variable to function: is it safe?

  28. 28

    returning static pointer to local variable from function

  29. 29

    Is it safe to pass pointer of a local variable to a channel in Golang?

HotTag

Archive