string operation and related exception

This is a discussion on string operation and related exception within the C++ Programming forums, part of the General Programming Boards category; As vart shows, it's a simple MessageBox....

  1. #46
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    As vart shows, it's a simple MessageBox.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #47
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    As you mentioned, assert in implemented by int 3, but I have not found code in assert (assert.h), which triggers int 3. Any ideas?

    Quote Originally Posted by Elysia View Post
    As vart shows, it's a simple MessageBox.

    Thanks vart,


    Quote Originally Posted by vart View Post
    something like
    Code:
    #undef  assert
    
    #ifdef  NDEBUG
    
    #define assert(exp)     ((void)0)
    
    #else
    
    #ifdef  __cplusplus
    extern "C" {
    #endif
    
    _CRTIMP void __cdecl _assert(void *, void *, unsigned);
    
    #ifdef  __cplusplus
    }
    #endif
    
    #define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0) )
    
    #endif  /* NDEBUG */
    <removed copyrighted code, sorry - CornedBee>

    So it is just an __crtMessageBoxA call followed by _DbgBreak to start debugging
    This is what I found from assert.h.

    Code:
    #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
    But it does not contain information as you mentioned, "followed by _DbgBreak", any ideas?


    regards,
    George

  3. #48
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by George2 View Post
    This is what I found from assert.h.

    Code:
    #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
    But it does not contain information as you mentioned, "followed by _DbgBreak", any ideas?


    regards,
    George
    This is UNICODE version of the same macro I have posted - you should look into the Assert.C file for _wassert function - I soppose It will have exactly the same code as the _assert function I have posted just using the wide-characters versions of string manipulation routines and MessageBox
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #49
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    I have read the related code, it works in the following way,

    1. generate and format message;
    2. If user select abort, int 3 will be generated;
    3. If user selects retry, debug break will be invoked.

    My questions,

    1. I do not know too much about int 3 and I can not find related information in MSDN and wikipedia, could you explain or recommend some materials about what will happen if we generate int 3 in our program?

    2. Interested how debugbreak works. When we invoke this function, what will happen? Goes back to next break point or go back to some place else?

    Quote Originally Posted by vart View Post
    This is UNICODE version of the same macro I have posted - you should look into the Assert.C file for _wassert function - I soppose It will have exactly the same code as the _assert function I have posted just using the wide-characters versions of string manipulation routines and MessageBox

    regards,
    George

  5. #50
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    #define _DbgBreak() __asm { int 3 }
    So _DbgBreak macro IS the place where the int 3 is called

    To read about interrupts - you can use the google search...
    http://www.google.co.il/search?num=1....microsoft.com
    http://msdn2.microsoft.com/en-us/library/cc266343.aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #51
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart!


    Great resource! I have made some learning, a further confusion.

    abort will raise(SIGABRT), i.e. sending signal SIGABRT, is signal SIGABRT the same as int 3 sent by DebugBreak?

    http://msdn2.microsoft.com/en-us/library/k089yyh0.aspx

    Quote Originally Posted by vart View Post
    Code:
    #define _DbgBreak() __asm { int 3 }
    So _DbgBreak macro IS the place where the int 3 is called

    To read about interrupts - you can use the google search...
    http://www.google.co.il/search?num=1....microsoft.com
    http://msdn2.microsoft.com/en-us/library/cc266343.aspx

    regards,
    George

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I haven't got the source code here, but I'm 99.9% sure that the code is essentially:
    Code:
       showerrormessage();
       DebugBreak();
       abort();
    abort() should "quit your program immediately".

    DebugBreak only really makes sense if you have a debugger registered on the system. It is not at all similar to abort(). Instead, it does exactly what it says: Breaks into the debugger, which means that you can for example see the call stack of how you got to the assert (or whatever contained the DebugBreak() call).

    Note: In the case of a Windowed application, I believe an assert shows a dialog box that allows the user to "quit" or "debug" [and a third choice, that I don't remember].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #53
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    "Ignore", which continues execution (and in the case of assert(), prompty falls into the abort().)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #54
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and CornedBee,


    1.

    So abort() and int 3 are two different things. about() will cause program to end and int 3 will search for debugger, right?

    2.

    I want to test the function of Continue, as mentioned here.

    http://msdn2.microsoft.com/zh-cn/lib...y6(en-us).aspx

    I have written the following progam, and I expect when click continnue button, we will be able to debug the code in __except block. But, when execute the following code, there is even no dialog displayed to let me select whether or not to use Visual Studio 2008 to debug. Why and how to fix it?

    Code:
    #include "Windows.h"
    
    int main()
    {
    	int a;
    	int b;
    	int c;
    
    	__try 
        {
    	
    		a = 100;
    		b = 200;
    		c = 300;
    
    		DebugBreak();
    		a = 400;
    	
        }
        __except(GetExceptionCode() == EXCEPTION_BREAKPOINT ? 
                 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
        {
            // No debugger is attached, so return FALSE 
            // and continue.
            return FALSE;
        }
    	
        return TRUE;
    }
    Quote Originally Posted by CornedBee View Post
    "Ignore", which continues execution (and in the case of assert(), prompty falls into the abort().)

    regards,
    George

  10. #55
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what happens in your case? Note that if you start from inside the IDE, the application may fall straight back to debugger.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #56
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    No Mats,


    In my environment, JIT debugger will not be invoked. Any ideas?

    Quote Originally Posted by matsp View Post
    So, what happens in your case? Note that if you start from inside the IDE, the application may fall straight back to debugger.

    --
    Mats

    regards,
    George

  12. #57
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Do you run from inside the debugger or outside?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #58
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    I run from command line. Any ideas to fix?

    Quote Originally Posted by Elysia View Post
    Do you run from inside the debugger or outside?

    regards,
    George

  14. #59
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Just to check, but JIT is enabled in Tools -> Options -> Debugging -> Just-in-Time, right? In Visual Studio?
    Because it works fine for me. If I trigger a debug break, Visual Studio shows the JIT debugger dialog.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #60
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Just to confirm two things,

    1. Do you use the code in post $54?

    2. Do you run from command line or from Visual Studio F5?

    Quote Originally Posted by Elysia View Post
    Just to check, but JIT is enabled in Tools -> Options -> Debugging -> Just-in-Time, right? In Visual Studio?
    Because it works fine for me. If I trigger a debug break, Visual Studio shows the JIT debugger dialog.

    regards,
    George

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Looking for examples for string related programs
    By koloth in forum C Programming
    Replies: 5
    Last Post: 04-14-2003, 11:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21