Click to See Complete Forum and Search --> : Problem with malloc(*void) and free(*void) in C / (better C) [urgent]
Stoodent
March 23rd, 2002, 09:15 AM
Hi, I'm a beginner to C/C++
I wrote a program for matrixoperations which should manage storage (memory) dynamicly.
The elements of a matrix should be float.
The problem is when allocating memory with malloc() I assign a float pointer to the return-value of malloc and when freeing the allocated memory I provide free with that pointer. I think that free() "thinks" that it just has to free a 4 byte memory block but indeed it is an array of floats to be freed.
What can I do ?
Here is a part of my sourcecode.
If you need more, please tell me:
//...
typedef struct
{
int nCol;
int nRow;
float* pContents;
} Matrix;
Matrix MatCreate(int m, int n)
{
Matrix z;
bool ok;
z.nCol = -1; z.nRow = -1; z.pContents = NULL;
if (m>-1 && n>-1)
{
z.pContents = (float*) malloc(z.nCol * z.nRow * sizeof(float));
ok = (z.pContents!=NULL);
if (ok)
{
z.nRow = m;
z.nCol = n;
}
}
return z;
}
//...
void MatDelete(Matrix z)
{
free(z.pContents);
}
//...
int main()
{
Matrix M1;
M1 = MatCreate(2,1);
//...
MatDelete(M1);
return 0;
}
Thanx 4 reading.
BenRush
March 23rd, 2002, 10:12 AM
First off, what makes you think that you're not freeing the right amount of memory?
Paul McKenzie
March 23rd, 2002, 01:36 PM
First, here is an implementation in both C and C++ of a matrix. It does not use a structure, but maybe by seeing how it is implemented, it will show you what you're doing wrong:
C:
http://www.codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=CPP&Number=6633&page=0&view=collapsed&sb=5
C++:
http://www.codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=vc&Number=172900&Search=true&Forum=vc&Words=Array&Match=Whole&Topic=&Searchpage=0&Limit=25
Second some advice:
There is no such language as "C/C++". You are either coding in 'C' or you are coding in C++. C and C++ are two separate languages, and C++ uses a different mode of thinking than for a C program. For example, usage of malloc() and free() are designated for C programs. The allocation functions for C++ are new and delete. Also a Matrix class written for C++ would most likely use a template or use STL vectors<> for the implementation (see the link above). Another example would be that a 'C' program would use char * to handle NULL terminated strings, while a C++ program would use std::string. If you were learning C++, you would never touch char * until you learned std::string. If you are learning 'C', you wouldn't learn std::string, since it doesn't exist in 'C'.
You have written a 'C' program. Yes it compiles on a C++ compiler, but C++ is a lot more than just taking a 'C' program and compiling it. If you want to learn C++, learn C++. By using old 'C' techniques like what you're doing now (malloc/free, no templates, no vectors), you are on the wrong track of learning proper C++ techniques. So either learn 'C', or learn C++ -- don't try to learn both at the same time.
Regards,
Paul McKenzie
Stoodent
March 23rd, 2002, 03:38 PM
Thank you very much for answering.
I found the mistake in the source. You will find it too if you look at the sourcecode I posted. I feel ashamed :-(
I'd like to say something to your second advise. I'm studing computer science at a german university of applied sciences and the following statements might be wrong because I actually learn C and C++ and that's what I have to do if I'd like to pass the tests but I'm sure I'm not that well in programing C or C++ as you can do. The stuff that I heared about C and C++ differs a little bit from that which you said but maybe I just get it wrong:
You're right, there is no language called "C/C++". I mean C and C++. When talking about C/C++ I mean both languages and they are seperate because they are built for different programming techniques where C++ is a midified C which provides tools to write in a objectorientated way. But C++ is a hybrid language which is object orientated as well as "imperative" (I don't know if this is the correct english expression.) where C is just "imperative" (no object-orientated). When talking about C you don't speak of one standard language. But if you mean ANSI/ISO C there is just one because it is standardized. Most C languages are totally compatible with "K&R C" which was the first C language (from Kerninghan and Richie).
Now talking about C as ANSI/ISO C and C++ as ANSI/ISO C++:
There are many elements of C which you can find in C++ but C is not a proper subset of C++ because there are things in C which you won't find in C++ and other things are deprecated because you better use a method from a class of a standart-library instead of a function of C which is not from a class
e.g. in C++ you can use printf(); insted of std::cout; because C++ is not only-object orientated as Java but it is hybrid.
Now in the thread where I asked the question I was talking about "Better C" or also called "Clean C" which you won't find as a standadized language. I think some people use the expression "Better C" or "Clean C" to say that they mean the proper subset of ANSI/ISO C and ANSI/ISO C++. You can say "Clean C" is equal to C++ if you don't use Classes etc. So you can use std::cout etc. in your program being forced not to use all the tools C++ provided for object-orientated programming. You might say std::cout is a method and it comes from a class (?!). Yes but using "Clean C" you won't built own classes and you just use it as you would write a C-program. We use the MS Visual C++ 6.0 compiler to write our programs. We have to know how to write a ANSI/ISO C program, a "Better C"-program and a ANSI/ISO C++ program.
I think it is good to learn both C and C++ at the same time as I am forced to do because you learn much about the differences. We formally learned Oberon if you know that (It is the new Modula2, coming from N. Wirth). I'm not new to programming. I first learned Basic on a C64 (15 years ago), then "Turo Pascal", Oberon, Java and now C and C++. I don't want to say that the things you said are wrong but in my opinion it is good to learn both languages at the same time.
Kind regards,
Mark.
NMTop40
March 24th, 2002, 01:11 PM
well the obvious error in your code is that you are allocating z.nRow * z.nCol which are still both -1 at the time so -1 * -1 is always 1 and not m * n.
It doesn't do any harm in this case, but if you write in C then normally you create structures and return a pointer to the structure (struct) rather than copying them.
However what you did was valid except for the error I pointed out.
The best things come to those who rate
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.