-2

I have Win7 Pro (32 bit) and CodeBlocks IDE. I would like to know is there any way to detect line with a segmention fault in C. My code is PRIME1.c I find somewhere on Stack Overflow that this is possible on linux in terminal, but I would like to do that in Windows. Could anyone tell me how to do that? Many thanks!

In other words, I would like to know how to use debugger from cmd in windows 7 and how it can tell me which line is problematic.

I just found this link Determine the line of C code that causes a segmentation fault? But, as you can see, this is for Linux. I would like to know how can I do that in Windows cmd?

Community
  • 1
  • 1

1 Answers1

0

You can catch seg fault. But, unfortunately, can not handle this event anyhow or get any info about that fault (in standard way, there are workarounds specific for compilers). So, maybe put printf in every line with __LINE__ macro and just wait until it fails.

#include <signal.h>
#include <conio.h>
#include <stdio.h>

void listener(int sig) {
    printf("listener: access violation");
    _getch();
}

void main() {
    char a = 10;
    char *p = &a;

    signal(SIGSEGV, listener);

    do {
        printf("%d", *p++);
    } while (1);

    _getch();
}
Ivan Ivanov
  • 2,076
  • 16
  • 33