Thread: Checking for runtime violations

  1. #1
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308

    Checking for runtime violations

    Hey. Is there a way I could enable runtime checking using some compilation flags for things like out_of_bounds access or signed integer overflow. Usually when I have such problems, my executable simply crashes. I've seen people (on streams) get messages like "Access Violation on line XX..." or something when there happens to be such errors but I haven't figured out how to enable this myself.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For out of bounds checking you would compile without defining NDEBUG and enable options like checked iterators, as well as placing your own pre-condition/post-condition assertions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    308
    I'm not sure I understand completely. I realised I was compiling without -D_GLIBCXX_DEBUG, after which, I get messages like (which is what I wanted):

    Code:
    c:\programming\mingw\mingw 9.2.0\include\c++\9.2.0\debug\vector:427:
    In function:
        std::__debug::vector<_Tp, _Allocator>::reference
        std::__debug::vector<_Tp,
        _Allocator>::operator[](std::__debug::vector<_Tp,
        _Allocator>::size_type) [with _Tp = int; _Allocator =
        std::allocator<int>; std::__debug::vector<_Tp, _Allocator>::reference =
        int&; std::__debug::vector<_Tp, _Allocator>::size_type = long long
        unsigned int]
    
    
    Error: attempt to subscript container with out-of-bounds index 10000, but
    container only holds 5 elements.
    
    
    Objects involved in the operation:
        sequence "this" @ 0x000000000023FD90 {
          type = std::__debug::vector<int, std::allocator<int> >;
        }
    
    
    This application has requested the Runtime to terminate it in an unusual way.
    Please contact the application's support team for more information.
    Upon further reading, I came across stuff like -fsanitize=address and -fsanitize=undefined. I'm not sure I understand how to make it work yet but could you give a brief description on what it's supposed to do?
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do the Access Violations every end?
    By durban in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2005, 10:13 PM
  2. Access Violations
    By nickname_changed in forum C++ Programming
    Replies: 0
    Last Post: 09-22-2003, 03:49 AM
  3. ShowWindow causing Access Violations - please help
    By Arrow Mk 84 in forum Windows Programming
    Replies: 8
    Last Post: 08-06-2002, 06:09 PM
  4. DJGPP violations...
    By kooma in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-08-2002, 12:44 PM
  5. access violations
    By curtner in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:32 AM

Tags for this Thread