C++ new[] into base class pointer crash on array access

前端 未结 5 736
南笙
南笙 2020-12-21 10:24

When I allocate a single object, this code works fine. When I try to add array syntax, it segfaults. Why is this? My goal here is to hide from the outside world the fact

5条回答
  •  离开以前
    2020-12-21 11:01

    Creating an array of 10 b's with new and then assigning its address to an a* is just asking for trouble.

    Do not treat arrays polymorphically.

    For more information see ARR39-CPP. Do not treat arrays polymorphically, at section 06. Arrays and the STL (ARR) of the CERT C++ Secure Coding Standard.

提交回复
热议问题