-3

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.

mol
  • 2,607
  • 4
  • 21
  • 40
  • 2
    How could we possibly know without seeing your code? – antonijn Mar 24 '13 at 10:34
  • I'm asking in general under which conditions such situation can corrupt a pointer. Unfortunately I can't show the code for some reasons. – mol Mar 24 '13 at 10:37
  • 1
    To expand on Antonijn's comment, I suppose we could try to list *all* of the things that could go wrong when you attempt to perform an invalid cast in an answer, but we'd be here all day and that would be completely unhelpful to everyone. It would be much easier and you would be much more likely to get a useful answer if you'd post the offending code. – Cody Gray - on strike Mar 24 '13 at 10:38
  • @mol And without seeing the code, we can't help you for some reasons. Sorry, but the only thing we can do now is guess, and that's not what so is for, and no-one will benefit from that. – antonijn Mar 24 '13 at 10:38
  • Related: [What could cause a dynamic_cast to crash?](http://stackoverflow.com/questions/278429/what-could-cause-a-dynamic-cast-to-crash) – Cody Gray - on strike Mar 24 '13 at 10:39
  • At the least, include the object inheritance code. Every `class` or `struct` that is a parent, child, or cousin of the class which you *think* you have. In addition, the value of `(int)(void*)ptr` and `((int*)ptr)[0]` through `((int*)ptr)[10]` before the crash can be helpful, as well as your compiler and version of your compiler and all compiler flags. – Yakk - Adam Nevraumont Mar 24 '13 at 10:55

1 Answers1

1

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

Alon
  • 1,776
  • 13
  • 31