Pregunta

I'm investigating a random crash (SIGBUS), and trying to find out what could possibly corrupt the pointer which invokes crashed method. I found that this pointer is casted from it's base class using C-style casting in C++ code. So, I wonder in what cases such a casting may cause a crash and try to understand if it was a root cause of my problem.

¿Fue útil?

Solución

The casting itself will not cause the crash(obviously..) But it is possible if you casted an object of a child that was not really that child that it will try to invoke a function from an un allocated or a wrong place and crash..

For example we have A, and B,C who inherit A,if B size is much grater than C.. you can crash it if you cast what was allocated C, to B. this can be resolved using dynamic_cast<SomeType>(ptr); and then asking if the result is not nullptr

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top