home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19529 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.2 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!metro!mama!radics!zoltan
  3. From: zoltan@research.canon.oz.au (Zoltan Kocsi)
  4. Subject: Re: The 2-Dee Array Deelemna
  5. In-Reply-To: almeida@ame.gsfc.nasa.gov's message of Wed, 6 Jan 1993 19:03:55 GMT
  6. Message-ID: <ZOLTAN.93Jan11113115@radics.research.canon.oz.au>
  7. Originator: zoltan@radics
  8. Sender: news@research.canon.oz.au
  9. Organization: Canon Information Systems Research, Australia
  10. References: <1993Jan6.190355.9632@nsisrv.gsfc.nasa.gov>
  11. Date: Mon, 11 Jan 1993 01:31:15 GMT
  12. Lines: 45
  13.  
  14. In article <1993Jan6.190355.9632@nsisrv.gsfc.nasa.gov> almeida@ame.gsfc.nasa.gov (aswin m. almeida) writes:
  15.  
  16.     [ text deleted ]
  17.     
  18.     for (i=0; i<10; i++)
  19.      for (j=0; j<nrows; j++)
  20.      {
  21.        printf("%d x %d = %d\n",i,j,i*j);
  22.        array[i][j] = i*j;   /* dbx reports this as the location of the bus error */
  23.        printf("%d,%d = %d\n",i,j,array[i][j]);
  24.     [ text deleted ]
  25.  
  26.    1) The reason I am getting this annoying bus error.
  27.    2) An explanation, with crude ASCII pictures, of *, **, and & in C.
  28.    3) An explanation of why | array[i] = (int *) malloc (ncolumns * sizeof(int));
  29.  
  30. 1) The bus error is caused by wrong order of indices, your array
  31.    ( with the given input ) is array[ 3 ][ 10 ] as you have an array that
  32.    has nrows elements containing nculumns ints each.
  33.  
  34. 2) and 3) In the memory it looks like this:
  35.  
  36.    --------------        
  37.    | array      |--+            |           |    
  38.    --------------  |            -------------
  39.    :            :  :            |array[0][9]|
  40.    --------------  |            -------------
  41.    | array[0]   |<-+            :           :
  42.    --------------\              -------------
  43.    | array[1]   |-\------------>|array[1][0]|
  44.    --------------  |            -------------
  45.    | array[2]   |--+--+         |array[1][1]|
  46.    --------------  |  |         -------------
  47.    :            :  :  |         |           |
  48.    --------------  |  |         -------------
  49.    | array[0][0]|<-+  |         |array[1][9]|
  50.    --------------     |         -------------
  51.    | array[0][1]|     |         :           :
  52.    --------------     |            -------------
  53.    |            |     +-------->|array[2][0]|
  54.  
  55.  
  56. Zoltan
  57. -- 
  58. Zoltan Kocsi <zoltan@research.canon.oz.au>
  59.