How to detect seg. faults?

How can i detect segmentation faults in my program without debugger (like dev-c++ does)?
Thanks.
If I don't have a debugger then I use log.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//logger.h
#ifndef LOGGER_H_
#define LOGGER_H_

    #ifdef DEBUG_MODE_ON
    #include <fstream>
    #include <string>
    #define LOG \
        { const std::string FUNCTION_NAME = __FUNCTION__; \
        char DEBUG_FILE[] = "debug_log.txt"; \
        if (FUNCTION_NAME == "main") \
        { \
            remove(DEBUG_FILE); \
        } \
        std::ofstream file(DEBUG_FILE, std::ios_base::app); \
        file << __FILE__ \
             << "\t" << __LINE__ \
             << "\t" << __FUNCTION__ << "\n";\
        file.close(); } \

    #else
    #undef LOG
    #define LOG ;
    #endif

#endif // LOGER_H_

Last edited on
I used log too. But how can i detect segmentation faults at runtime? For example, my program generates seg. fault and then prompts what to do (and there won't be that stupid "send error report" message from windows)
You can't. The OS (e.g. NT kernel, Linux kernel, BSD kernels) will always detect them and crash the guilty program. There are, however, memory debuggers that can detect problems such as buffer overruns and uninitialized variables as soon as they occur.
Well, technically with Un*x you can catch the SIGSEGV signal, if you dare, but it is rarely useful to do so.
Thank you Duoas! I also found example here:
http://msdn.microsoft.com/en-us/library/s58ftw19.aspx
Topic archived. No new replies allowed.