I had some code that looked something like the following:
struct fooclass {
char data[32];
}
fooclass makefoo() {
fooclass res;
// for example:
memset(res.data, 0, sizeof(res.data));
res.data[0] = 43;
return res;
}
struct do_stuff {
const fooclass& obj;
do_stuff(const fooclass& obj_in) : obj(obj_in) { }
void operate() { /* do things with obj; */ }
}
bool put_it_to_work(struct do_stuff& f) {
f.operate();
f.operate();
return true;
}
bool call_it_all() {
do_stuff local(makefoo());
return put_it_to_work(local);
}
With gcc, the code worked fine. With MSVC2012, local.obj
became corrupted.
The question is: how long does the fooclass
returned by makefoo
last? Should it be until the end of call_it_all
, or is it just until the end of the do_stuff local(makefoo())
line and I just got lucky? I'd appreciate a reference to the standard.