3

When I try to convert a vector of integers to a array of integers inside a loop although within the loop the conversion appears to have worked, once the loop terminates the value within the array is incorrect.

I tried using std::copy instead of &vector[0], and the same problem arose. I am aware that converting the vector to an array is not necessary. I assume the problem is that the array is a pointer to a variable that is destroyed after the loop, but I'm fairly new to C++, so even if I am correct, I don't know how to fix it.

#include "pch.h"
#include <iostream>
#include <vector>

int main()
{
    int* arr;
    for (int i = 0; i < 1; i++)
    {
        std::vector<int> vec{ 1 };
        arr = &vec[0];
        std::cout << "Inside the loop in the vector: " << vec[0] << std::endl;
        std::cout << "Inside the loop in the array: " << arr[0] << std::endl;
    }
    std::cout << "Outside the loop in the array: " << arr[0];
}

I would expect the output to look like this:

Inside the loop in the vector: 1
Inside the loop in the array: 1
Outside the loop in the array: 1

But it actually turns out like this:

Inside the loop in the vector: 1
Inside the loop in the array: 1
Outside the loop in the array: -572662307

14
  • 3
    Possible duplicate of Can a local variable's memory be accessed outside its scope? Commented May 31, 2019 at 13:10
  • @AlgirdasPreidžius it is not the access to a local var but the access to a freed block of memory in the heap Commented May 31, 2019 at 13:19
  • @bruno It's basically the same thing. Commented May 31, 2019 at 13:21
  • 1
    @bruno "saying stack and heap are the same" 1) I didn't state this. Don't put words in my mouth. 2) Technically, there is no such concepts as stack, or heap, in C++ standard, either. It's just those are typical implementations. Commented May 31, 2019 at 13:28
  • 1
    @bruno The "local" part of that question title is not the point anyway. The Q&A is very informative and completely applicable. Let's agree to disagree. Commented May 31, 2019 at 13:56

2 Answers 2

6

int* arr; <- This is a pointer, not an array.

std::vector<int> vec{ 1 }; <- This vector only exists within the body of your loop. That means, every iteration a new vector is created, and at the end of the iteration it is destroyed, freeing the memory it allocated.

arr = &vec[0]; <- This line doesn't copy anything, it simply sets the address that arr points to, to the first element in the vector's allocated memory.

Since your vector is destroyed after the loop, the address arr points to is invalid. As long as you are inside the loop, the address it points to is valid and you can safely access that memory.

You were right to try std::copy, however if you want to copy anything, you need to have memory allocated first that you can copy into. int arr[1]; would create an array of int with size 1, which can be used as the destination in std::copy. int* arr = new int[1]; would also work, but requires that you also free the memory later yourself (delete[] arr;). I wouldn't recommend managing memory yourself except for learning purposes.

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

Comments

1

The storage for vec[0] is freed when vec falls out of scope. You wouldn't expect a pointer to it to be valid outside the scope in which the vec variable lives.

I suggest you read this answer.

1 Comment

the suggest answer is not adapted to the problem, it is not the access to a disappeared local var

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.