Home > Enterprise >  Assigning the reference of a stack allocated variable to a pointer
Assigning the reference of a stack allocated variable to a pointer

Time:01-05

Given the following function:

void test(queue<string>* out) {
   queue<string> abc = queue<string>();
   abc.push("abc");
   out = &abc;
}

Theoretically, the abc variable is allocated on the stack and at the end of the function it must automatically pop out of the stack. But I am assigning the reference of that variable to the out parameter which is a pointer. So when the context which calls this function pass a pointer to capture the output, what would happen? In other words, is this operation safe at all?

To make the question very clear, is the following function equivalent to the above:

void test(queue<string>* out) {
   out->push("abc");
}

CodePudding user response:

The first function is safe, but not equivalent to the second.

You are just assigning a value to the pointer out which is a local variable. That local variable is not connected to the caller and the assignment is not observable by the caller. Your function has no side-effects. It is equivalent to

void test(queue<string>* out) {}

You have not stored a pointer of the abc object anywhere reachable from outside the function.

If you had done so, that pointer would be dangling after the function call returns, because the abc object will be destroyed at that point. Whether you hold a reference/pointer to it does not matter at all. Variables declared at block scope (without static or thread_local specifiers) have automatic storage duration, meaning that they will be destroyed at the end of the block in which they are declared, always.

Dereferencing the dangling pointer would then cause the program to have undefined behavior, meaning that you lose any guarantees on the program behavior.

For this reason it is a clear mistake to return a reference or pointer to a local variable, whether through the return value or out-parameters.

CodePudding user response:

So when the context which calls this function pass a pointer to capture the output, what would happen?

Its unclear what you mean by "capture the output". The function has no output.

In other words, is this operation safe at all?

The problem isn't so much with safety, rather the function doesn't do anything useful.

But I am assigning the reference of that variable to the out parameter which is a pointer.

This is fine because the pointer parameter itself doesn't have a (significantly) longer lifetime.

Is the following function equivalent to the above:

The functions aren't equivalent. The first function doesn't indirect through the pointer parameter and doesn't modify the queue that is pointed.

CodePudding user response:

No, it does not. I'd rather rewrite like this:

using StringQueue = std::queue<std::string>;
void test( StringQueue& out) {
   out.push("abc");
}

CodePudding user response:

No, it does not. I'd rather rewrite like this:

using StringQueue = std::queue<std::string>;
void test( StringQueue*& out ) {
   out = new StringQueue;
   out->push( "abc" );
}
  •  Tags:  
  • Related