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

  1. Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!tamsun.tamu.edu!news
  2. From: jrh3870@tamsun.tamu.edu (John R Harper)
  3. Newsgroups: comp.lang.c
  4. Subject: HELP! (with 2D dynamic allocation)
  5. Date: 12 Jan 1993 10:59:15 -0600
  6. Organization: Texas A&M University, College Station, TX
  7. Lines: 75
  8. Distribution: world
  9. Message-ID: <1iutd3INNmk0@tamsun.tamu.edu>
  10. NNTP-Posting-Host: tamsun.tamu.edu
  11.  
  12.   hello out there:
  13.  
  14.   (first, the disclaimer: i'm a novice in C, so please excuse any 
  15. pinheadedness that follows.) 
  16.  
  17.   I have been using the following code snippet from the FAQ sheet to allocate a 2D array as follows:
  18.  
  19.    int **data;
  20.    [...]
  21.    data = (int **)malloc(ydim * sizeof(int));
  22.    data[0] = (int *)malloc(xdim * ydim * sizeof(int));
  23.    for(i = 1; i < ydim; i++){
  24.       data[i] = data[0] + i * xdim;
  25.    }
  26.  
  27. which seems to work like a charm, since it is followed by
  28.  
  29.    read(fp, &data[0][0], num_of_bytes);  /* this works ...     */
  30.  
  31. this (i think) just places a chunk of data (size num_of_bytes) into the contig-
  32. uous dynamic array data[i][j]. 
  33.  
  34.   *now*, this array data[i][j] is passed to a function as follows:
  35.  
  36.    main(){
  37.    [...]
  38.    Function(data, xstart, ystart, xend, yend);  /* called within main() */
  39.                                                 /* declared in separate */
  40.                                                 /* module               */
  41.    [...]
  42.    }
  43.  
  44. then inside a _separate_ module:
  45.  
  46.    Function(data, x_start, y_start, x_end, y_end)   /* start separate module */
  47.    unsigned short **data;
  48.    short x_start, y_start, x_end, y_end; /* these let you process any      */
  49.    {                                     /* subset of the array if need be */
  50.    [...]
  51.    mean = 0;
  52.    for(i = x_start; i < x_end; i++){
  53.       for(j = y_start; i < y_end; j++)
  54.          mean = mean + *(data[i] + j);
  55.    }
  56.    [...]
  57.    }
  58.  
  59.    then this is compiled (on an HP Apollo 400 series) as follows:
  60.  
  61.    % cc -c function.c 
  62.    % cc -o main main.c function.o -lm 
  63.  
  64.    all the code above works on *square* arrays (tested up to 512 x 512). 
  65. however, it crashes on *rectangular arrays* (for instance 128 x 21) with the
  66. following error message:
  67.  
  68.    % segmentation fault (core dumped)   /* for the millionth time ... */ 
  69.  
  70.    i have placed a printf() inside the averaging loop above, telling me when
  71. each element of *(data[i] + j) is accessed. the programs chugs along until it
  72. gets up to data[50][0]. then the error occurs. 
  73.  
  74.    i seem to recall having this same problem in main() (with the read statement)until i made sure the allocated array was contiguous; but i could be mistaken. 
  75.  
  76.    any ideas? hopefully this has been coherent -- but if not, i'd be happy to
  77. correspond via email if anyone is interested. any feedback would be *greatly*
  78. appreciated. thanks!!
  79.  
  80.   j harper 
  81.  
  82. -- 
  83. +-----------------------------------------------------------------------------+
  84. | john harper               |   Department of Physics  Texas A & M University |
  85. | jrh3870@tamsun.tamu.edu   |                    College Station, Texas 77843 |
  86. +-----------------------------------------------------------------------------+
  87.