1

The error in the following code is that memcpy(t[j], m[j], sizeof(int) * DIM * DIM); should be memcpy(t[j], m[j], sizeof(int) * DIM);:

// Untitled7.c

#include <stdio.h>
#include <string.h>

#define DIM 1000

int main(void)
{
    int m[DIM][DIM], t[DIM][DIM];
    unsigned j, k;

    for(j = 0; j < DIM; j++)
        for(k = 0; k < DIM; k++)
            m[j][k] = j * DIM + k;

    for(j = 0; j < DIM; j++)
        memcpy(t[j] ,m[j], sizeof(int) * DIM * DIM); // only one DIM

    for(j = 0; j < DIM; j++)
        for(k = 0; k < DIM; k++)
            m[j][k] = t[DIM-k-1][j];

    return 0;
}

Using gdb how could I find that error? I understood how to create the core file (using ulimit -c unlimited), then I used $ gdb Untitled7 core and it gives:

...

Reading symbols from Untitled7...done.
[New LWP 10610]
Core was generated by ` ?  @  A'. // and symbols of binary files
Program terminated with signal SIGSEGV, Segmentation fault.
#0  __memcpy_ssse3 () at ../sysdeps/i386/i686/multiarch/memcpy-ssse3.S:2590
2590    ../sysdeps/i386/i686/multiarch/memcpy-ssse3.S: File o directory non esistente. // File or directory not existent.

After what should I do?

untitled
  • 389
  • 4
  • 13
  • 4
    You can't really use GDB to find these kind of errors. I suggest you use [Valgrind](http://valgrind.org/) instead. – Some programmer dude Feb 07 '16 at 15:02
  • 1
    Related: [Recommended way to track down array out-of-bound access/write in C program](http://stackoverflow.com/questions/24284293/recommended-way-to-track-down-array-out-of-bound-access-write-in-c-program) – Mark Plotnick Feb 07 '16 at 16:59

1 Answers1

1

You cannot always find the real error this way, but you can find where the segmentation violation has occurred.

You need to compile your program with debugging information and preferably with no optimisations, i.e. use -ggdb -O0 compilation flags. If the violation is inside a library function, like in your example, use the gdb command up to move up the call stack until you arrive to your code. You should then see the offending line of the program.

Make sure gdb can find your sources. Normally when your current directory is your build directory, gdb can find them. If not, use the directory command of gdb.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243