Wednesday, December 11, 2024

Normally it's the other way around

 More from the department of bogus C++ behavior.

There is a function in a mobile C++ project that makes a local copy of a rather large global POD data structure. The copy is read only; a const reference would do just fine, the author probably meant to use a reference but mistyped. This fragment crashes in debug builds but works in release builds. We all have heard stories about the converse, when something works in debug but crashes with optimizations; but in this case, the optimized build replaces a copy with a reference, while the debug build executes as written and overflows the stack.

Not a bug. Not an undefined behavior. I've seen compilers handle overlarge data in the stack frame gracefully (by quietly allocating a heap block in the function prologue); not this time. On top of everything else, that fragment was executing on a framework managed thread, where I'm not sure stack size can be explicitly specified; and even if it was, I'm not sure the OS would have honored a request for an overlarge stack.

No comments:

Post a Comment