简体   繁体   中英

C++: Allocation of variables in a loop

Let's say I have a loop like this:

vector<shared_ptr<someStruct>> vec;
int i = 0;
while (condition)
{
     i++
     shared_ptr<someStruct> sps(new someStruct());
     WCHAR wchr[20];
     memset(wchr, i, 20); 
     sps->pwsz = wchr;
     vec.push_back(sps);
}

At the end of this loop, I see that for each sps element of the vector, sps->pwsz is the same. Is this because I'm passing a pointer to memory allocated in a loop, which is destructed after each iteration, and then refilling that same memory on the next iteration?

I don't think this code does what you expect it to. You probably want wchr created on the heap or as a member of someStruct .

wchr is allocated on the stack and gets freed on each iteration. The stack location will be the same on each iteration (probably the same).

Each of your smart pointers contains a pointer to that invalid memory address.

After your while loop all of your smart pointers point to that same invalid already freed address. The address was re-used and freed on each iteration.

sps->pwsz is a pointer to a char buffer. You give each new sps instance a pointer to the same memory location.

Even worse, that memory location is de-allocated after the loop ends, and will likely be re-used by other code quickly. It is invalid outside the while loop.

To fix this,

  WCHAR * wchr = new WCHAR [20];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM