home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!think.com!spool.mu.edu!umn.edu!pebi
- From: pebi@aem.umn.edu (Peter A. Bidian)
- Subject: Q: Structures, arrays, malloc and free...
- Message-ID: <pebi.721495272@zephyr.aem.umn.edu>
- Sender: news@news2.cis.umn.edu (Usenet News Administration)
- Nntp-Posting-Host: zephyr.aem.umn.edu
- Organization: University of Minnesota
- Date: Wed, 11 Nov 1992 15:21:12 GMT
- Lines: 71
-
-
- Hi!
-
- Sorry to use all that bandwidth, but I have a problem that drives me
- bonanza...
-
- I define matrices as structures:
-
-
- struct matrix{
- unsigned rows;
- unsigned cols;
- double **elem;
- };
-
- Using in a header these definitions, I assign memory to the arrays in the
- definition:
-
- double **memmat(unsigned, unsigned);
- void freemat(struct matrix);
-
- This is a function that initializes the matrix.
-
- struct matrix matdef(unsigned rows, unsigned cols)
- {
- struct matrix m;
-
- m.rows=rows;
- m.cols=cols;
-
- m.elem=memmat(m.rows,m.cols);
-
- return m;
- }
-
-
- This all works well. But the problem starts, when I try to free all these
- allocated matrices. I use the freemat above as a call to free a certain
- matrix, in fact only its array **elem asigned before. That is:
-
-
- void freemat(struct matrix m)
- {
- int i;
-
- for (i=m.cols-1; i>=0; i--) free((char*) m.elem[i]);
- free((char*) m.elem);
- }
-
- This doesn't seem to work at all. I get buss errors and so on. I tried
- to change the (char *) to (double *), or let it go alltogether. I tried
- to pass the matrix m as a pointer to it in freemat.... nothing worked.
-
- Does anybody have any suggestions? I would be thankfull if you could
- reply by mail. I don't know of how much interest this is for the general
- public. .... And another point to this topic. If I have a function that
- adds to matrices and returns the result: I define the resulting matrix
- in the function as above. My question is: every time I call the function
- will this definition allocate more and more memory, or will it work on the
- same chunk of memory. I.e. if I call that function 200 times, will
- the program allocate finally 200 different chunks of memory of that
- size, or will it use a earlier chunk.
-
- Thanks a lot,
-
- Peter
-
- ... hoping to recover from "sleepless" nights... :-)
-
-
-
-