Thread: Strange MinGw Windows runtime error

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    109

    Exclamation Strange MinGw Windows runtime error

    Strange MinGw Windows runtime error-sigtrap-jpgI have this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
        int (*a)[5][5] = calloc(25, 1);
        *a[0][0] = 5;
    
        size_t i, j;
        for(i =0; i < 5; i++)
        {
            for(j = 0; j < 5; j++)
            {
                *a[i][j] = 25;
            }
        }
        printf("%d\n", *a[0][4]);
        free(a);
    
        return EXIT_SUCCESS;
    }
    When run, it just does nothing and exits. I used MinGW gdb as shown in the attached screenshot and I'm getting a SIGTRAP ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed () error. At first I thought maybe some memory corruption was going on but when I examined the array, it is successfully populated and this crash happens AFTER the loop when the printf statement is run.

    Does anyone have experience with this error or know what it may be?
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,317
    Well you're not allocating anywhere near enough memory for a start.

    Try
    int (*a)[5][5] = calloc(25, sizeof(int));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    Besides that, your repeated usage of *a[0][0] and the like is incorrect. It should be (*a)[0][0], and similiar. Your compiler should have warned you, and if it didn't, try compiling at a higher warning level.
    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

  4. #4
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    109
    Gah. Well strangely even with -Wall it still doesn't flag anything and builds just fine.

    But that was the problem. Made those 2 changes and it actually builds and runs way faster, and works correctly now.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,937
    Oh, my bad. I forgot that that would look fine to the compiler: array index notation is ultimately syntactic sugar for pointer notation. It will just do the index computations as per normal, and as long as you happen to remain within the bounds, it's okay. It's just that the computed index isn't going to be what you expect, e.g., *a[i][j] is equivalent to **(*(a + i) + j), which means that for i > 0, you exceed the bounds of the array (this array isn't the array of 5 arrays of 5 ints; it's a hypothetical array of an unknown number of arrays of 5 arrays of 5 ints, of which this array you allocated is the "first element").

    Put it another way: if we "flatten" this hypothetical 3D array into a 1D array, then *a[i][j] i.e., **(*(a + i) + j) would be equivalent to a[25 * i + 5 * j + 0], whereas you presumably want the equivalent of a[5 * i + j].

    Therefore, my advice would be to not to mess with pointers to entire arrays unless you're really traversing across a multi-dimensional array using that pointer. If not for this pointer to a 2D array itself, we wouldn't have to consider this notion of a hypothetical 3D array.
    Last edited by laserlight; 06-08-2019 at 10:35 PM.
    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

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    109
    Gotcha, thanks. Yeah sounds like a recipe for difficult-to-find bugs!

    On another note, does anyone know more about
    ntdll!RtlIsNonEmptyDirectoryReparsePointAllowed? I'm going to go and look into it but this isn't the first time I've gotten that. It always fascinates me when I get an error like that instead of a segfault. Basically it seems that I caused a segfault in a Windows library.
    Last edited by Asymptotic; 06-08-2019 at 11:50 PM.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange linker error with minGW and boost
    By pheres in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2009, 08:45 PM
  2. Strange error during runtime
    By JamesKidder in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 07:49 PM
  3. C++ runtime, dynamic link, mingw.
    By 39ster in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2007, 12:00 PM
  4. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 08:15 AM
  5. runtime error, windows XP, borland C++ 5.5
    By finnepower in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2005, 12:29 PM

Tags for this Thread