-2

I'm currently deep-diving into the way pointers work.

Something for me unexplainable happened when executing the following lines of code:

 std::vector<OptimizerPlanOperatorPtr> sources; for (const auto &source : sourceOperators){ OptimizerPlanOperator planOperator = OptimizerPlanOperator(source); sources.push_back(static_cast<std::shared_ptr<OptimizerPlanOperator>(&planOperator)); } 

all sourceOperators differ, however when checking the elements of sources, they all point to the same OptimizerPlanOperator.

When I ran the debugger, I realized that in every loop step, all values of sources change to the recent value.

My assumption is, that I poorly initialized the pointer here which somehow results in the value the pointer refers to being overridden.

Can somebody show a solution or explain, what I did wrong here?

3
  • 1
    What is OptimizerPlanOperatorPtr? What are you trying to do with that static_cast over there? Commented Mar 31, 2022 at 11:58
  • 1
    Side note: "overridden" != "overwritten", even though they sound alike in American. Commented Mar 31, 2022 at 12:08
  • 1
    OptimizerPlanOperator(source) probably should return a std::shared_ptr<OptimizerPlanOperator> so then you would have auto planOperator = OptimizerPlanOperator(source); and sources.push_back(planOperator); and no dangling pointer or trying to free a stack variable Commented Mar 31, 2022 at 12:11

2 Answers 2

4

You are storing the location of an object whose lifetime ends with the current iteration and handing ownership of it to a shared_ptr. Both are problems that lead to undefined behaviour.

Casting a pointer to std::shared_ptr does not automagically make the pointed-to object into a shared object and extend its lifetime, and it is equivalent to std::shared_ptr<OptimizerPlanOperator>(&planOperator).

The simplest solution is to not do this stepwise but all at once:

for (const auto &source : sourceOperators){ sources.push_back(std::make_shared<OptimizerPlanOperator>(source)); } 
Sign up to request clarification or add additional context in comments.

Comments

3

Your planOperator is a local variable on the stack, and when you are passing it to the cast static_cast<std::shared_ptr<OptimizerPlanOperator>(&planOperator), you are passing in the address of a local variable, so as soon as the iteration is over, that pointer &planOperator becomes garbage.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.