none
detect array out of bounds RRS feed

  • Question

  • Visual Studio 2008, Windows XP, C++

    How do I set up Visual Studio to detect a software error that writes or reads outside the bounds of an array?  I expect the debugger to stop the code when it tries to read or write to an array location that is not valid.


    ~jag77 We need to know what a dragon is before we study its anatomy. (Bryan Kelly, 2010)
    • Moved by Larcolais Gong Monday, November 8, 2010 7:09 AM C++ (From:Visual Studio Setup and Installation)
    Friday, November 5, 2010 5:24 PM

Answers

  • If you're talking about plain C/C++ arrays like in

    int *p = new int[100]; p[101] = 42;

    then there's no such thing as out of bounds detection in Visual C++.

    If you're talking about STL containers like std::vector then out of bounds detection is enabled by default when you compile in debug mode.

     

    • Marked as answer by JAG77 Wednesday, November 10, 2010 3:08 AM
    Monday, November 8, 2010 8:26 AM
    Moderator
  • Hi JAG77,

    No. VC++ doesn’t check the bound of the Array. When the index is out of bound, you may write to or read from a unallocated memory.  If the memory block is used by others, you may get the garbage data or receive an access validation. Therefore we have to take care of the bound of the array by ourselves.

     

    However, you can use std::vector  or std::array (in VS2010) instead of the basic array in VC++. The std::vector and std::array is implemented for popping an exception when the index is out of bound.

     

    If you have any concern, please let me know.

     

    Cheers,

    Yi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by JAG77 Monday, November 8, 2010 8:14 PM
    Monday, November 8, 2010 8:48 AM
    Moderator

All replies

  • Hi JAG77,

     

    Thank you for posting. Based on your description, your concern is off topic here. Here is Visual Studio installation and Setup forum. I will help you moving your thread into the appropriate forum. Since more expert about your topic provide more helpful suggestions there.

     

    Regards,

    Larcolais


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, November 8, 2010 7:08 AM
  • If you're talking about plain C/C++ arrays like in

    int *p = new int[100]; p[101] = 42;

    then there's no such thing as out of bounds detection in Visual C++.

    If you're talking about STL containers like std::vector then out of bounds detection is enabled by default when you compile in debug mode.

     

    • Marked as answer by JAG77 Wednesday, November 10, 2010 3:08 AM
    Monday, November 8, 2010 8:26 AM
    Moderator
  • Hi JAG77,

    No. VC++ doesn’t check the bound of the Array. When the index is out of bound, you may write to or read from a unallocated memory.  If the memory block is used by others, you may get the garbage data or receive an access validation. Therefore we have to take care of the bound of the array by ourselves.

     

    However, you can use std::vector  or std::array (in VS2010) instead of the basic array in VC++. The std::vector and std::array is implemented for popping an exception when the index is out of bound.

     

    If you have any concern, please let me know.

     

    Cheers,

    Yi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by JAG77 Monday, November 8, 2010 8:14 PM
    Monday, November 8, 2010 8:48 AM
    Moderator
  • I was really expecting to find an option to do just that. 

    Thanks for taking the time to answer.


    ~jag77 We need to know what a dragon is before we study its anatomy. (Bryan Kelly, 2010)
    Monday, November 8, 2010 8:14 PM
  • Even in 2010, this was incorrect. You can extend your array by a uuid + null. Then check if your pointer ever begins at that uuid + null. Then log a warning anytime it is hit (because it is super improbable...but is possible). It is difficult to implement after-the-fact.

    There are commercial and open-source tools that will assist in debugging Visual C++ for these issues.

    Friday, September 27, 2019 8:04 PM