#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0

    What's the error?


    Hi,
    I have the following array, which doesn't want work. After I debug, it prints out nothing and shows an application error.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
    {
    int nr=4, nc=6;

    float** pmtx = (float**)malloc(nr*sizeof(float*));
    for (int i=0; i<nr; ++i)
    pmtx[i] = (float*)malloc(nc*sizeof(float));

    for (int i=0; i<nr; ++i)
    for (int j=0; i<nc; ++j)
    pmtx[i][j] = (i+1)*(j+1);

    for (int i=0; i<nr; ++i)
    {
    for (int j=0; j<nc; ++j)
    printf("%8.lf", pmtx[i][j]);
    printf("\n");
    }
    printf("\n");

    for (int i=0; i<nr; ++i)
    free (pmtx[i]);
    free (pmtx);
    return 0;
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Here is why you should use code tags when posting code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int nr = 4, nc = 6;
    
      float **pmtx = (float **) malloc(nr * sizeof(float *));
      for (int i = 0; i < nr; ++i)
        pmtx[i] = (float *) malloc(nc * sizeof(float));
    
      for (int i = 0; i < nr; ++i)
        for (int j = 0; i < nc; ++j)
          pmtx[i][j] = (i + 1) * (j + 1);
    
      for (int i = 0; i < nr; ++i) {
        for (int j = 0; j < nc; ++j)
          printf("%8.lf", pmtx[i][j]);
        printf("\n");
      }
      printf("\n");
    
      for (int i = 0; i < nr; ++i)
        free(pmtx[i]);
      free(pmtx);
      return 0;
    }
    Now, debugging is more than just running the code and watching it crash.
    Debugging means run the code in the debugger and have a good look around to see what caused the problem
    Code:
    $ gdb ./a.out 
    GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    For bug reporting instructions, please see:
    <http://bugs.launchpad.net/gdb-linaro/>...
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Program received signal SIGSEGV, Segmentation fault.
    0x000000000040067b in main () at foo.c:14
    14	      pmtx[i][j] = (i + 1) * (j + 1);
    (gdb) print i
    $1 = 0
    (gdb) print j
    $2 = 33776
    (gdb) list
    9	  for (int i = 0; i < nr; ++i)
    10	    pmtx[i] = (float *) malloc(nc * sizeof(float));
    11	
    12	  for (int i = 0; i < nr; ++i)
    13	    for (int j = 0; i < nc; ++j)
    14	      pmtx[i][j] = (i + 1) * (j + 1);
    15	
    16	  for (int i = 0; i < nr; ++i) {
    17	    for (int j = 0; j < nc; ++j)
    18	      printf("%8.lf", pmtx[i][j]);
    (gdb)
    The first output shows the segfault, AND the line of code causing the problem.

    OK, so i and j are involved, so print them out
    Woah - WTF is that huge number in j, where did that come from!?

    Now print the code around the offending line - do you see it yet?
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    17
    Rep Power
    0
    Thank you, and sorry for not formatting.
    Code:
     
    #include <stdio.h>
    
    void main ()
    {
    printf("this is how to use this code tag, cool\n");
    }

IMN logo majestic logo threadwatch logo seochat tools logo