home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16328 < prev    next >
Encoding:
Text File  |  1992-11-11  |  2.1 KB  |  83 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!think.com!spool.mu.edu!umn.edu!pebi
  3. From: pebi@aem.umn.edu (Peter A. Bidian)
  4. Subject: Q: Structures, arrays, malloc and free...
  5. Message-ID: <pebi.721495272@zephyr.aem.umn.edu>
  6. Sender: news@news2.cis.umn.edu (Usenet News Administration)
  7. Nntp-Posting-Host: zephyr.aem.umn.edu
  8. Organization: University of Minnesota
  9. Date: Wed, 11 Nov 1992 15:21:12 GMT
  10. Lines: 71
  11.  
  12.  
  13. Hi!
  14.  
  15. Sorry to use all that bandwidth, but I have a problem that drives me
  16. bonanza...
  17.  
  18. I define matrices as structures:
  19.  
  20.  
  21.     struct matrix{
  22.         unsigned rows;
  23.         unsigned cols;
  24.         double **elem;
  25.         };
  26.  
  27. Using in a header these definitions, I assign memory to the arrays in the
  28. definition:
  29.  
  30.     double **memmat(unsigned, unsigned);
  31.     void freemat(struct matrix);
  32.  
  33. This is a function that initializes the matrix.
  34.  
  35.     struct matrix matdef(unsigned rows, unsigned cols)
  36.     {
  37.         struct matrix m;
  38.     
  39.         m.rows=rows;
  40.         m.cols=cols;
  41.     
  42.         m.elem=memmat(m.rows,m.cols);
  43.     
  44.         return m;
  45.     }
  46.  
  47.  
  48. This all works well. But the problem starts, when I try to free all these
  49. allocated matrices. I use the freemat above as a call to free a certain
  50. matrix, in fact only its array **elem asigned before. That is:
  51.  
  52.  
  53.     void freemat(struct matrix m)
  54.     {
  55.         int i;
  56.  
  57.         for (i=m.cols-1; i>=0; i--) free((char*) m.elem[i]);
  58.         free((char*) m.elem);
  59.     }
  60.     
  61. This doesn't seem to work at all. I get buss errors and so on. I tried
  62. to change the (char *) to (double *), or let it go alltogether. I tried
  63. to pass the matrix m as a pointer to it in freemat.... nothing worked.
  64.  
  65. Does anybody have any suggestions? I would be thankfull if you could 
  66. reply by mail. I don't know of how much interest this is for the general
  67. public. .... And another point to this topic. If I have a function that
  68. adds to matrices and returns the result: I define the resulting matrix
  69. in the function as above. My question is: every time I call the function
  70. will this definition allocate more and more memory, or will it work on the
  71. same chunk of memory. I.e. if I call that function 200 times, will
  72. the program allocate finally 200 different chunks of memory of that
  73. size, or will it use a earlier chunk.
  74.  
  75. Thanks a lot,
  76.  
  77. Peter
  78.  
  79. ... hoping to recover from "sleepless" nights... :-)
  80.  
  81.  
  82.  
  83.