|
From: Ioannis V. <no...@ya...> - 2001-09-16 02:02:21
|
I write this because you said to point out the mistakes. :) =20 It is not new() and delete(), they are operators new and delete ( and new[] and delete[]). =20 Then in your code you do p=3Dnew int[10] and delete p; but it should be delete[] p; =20 Also new, new[], delete and delete[] are better because you can define what to do in memory allocation failure of new or new[] using set_new_handler() and overiding them to define where to place their objects (placement operators) in already allocated memory (even in the stack), which is a bit advanced topic. =20 =20 Regards, Ioannis =20 * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com <http://www.noicys.f2s.com/>=20 * Alternative URL: http://run.to/noicys -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Jason Hardman Sent: Sunday, September 16, 2001 1:42 AM To: dev...@li... Subject: Re: [Dev-C++] Hello I'm afraid I can't offer you much help with OpenGL and those tutorials, but I do know something about "free()". =20 When programming in C you use free() allong with malloc() to dynamically create and *free* up memory. If you want to learn more about dynamic allocation you should read a good C/C++ book. Any C book should explain more about malloc() and free(). =20 From what you have said it sounds like you need to include <stdlib.h>, which is where those functions are declared. If it is already defined then the problem is elsewhere. =20 As for alternatives... if you are using C++ (as aposed to pure C) then you can use the dynamic allocation functions that are part of the C++ language, which are new() and delete(). Again any good C++ book will explain these. Using malloc() and free() is still valid in C++ (and you can use the header <cstdlib> instead of <stdlib.h>), but new() and delete() are better because of the way they are designed to handle pointers and datatypes as part of the language. =20 i.e. =20 int *p; p =3D new int[10]; // create an int array =20 for(int i=3D0; i<10; ++i) p[i] =3D i*i; // fill array with square of 0 through to 9. =20 for(int i=3D0; i<10; ++i) cout << p[i] << endl; // display contents of dynamic array. // don't forget need to include <iostream> to use cout etc... =20 delete p; // always free up memory or you will get a memory leak!!! ... =20 This is just a quickie I created off the top of my head, so it probably won't work ;-) I'm too lazy to actualy test it. =20 p =3D new int[10]; =20 is the same as =20 p =3D (int*) malloc(10*sizeof(int)); =20 in C. =20 delete p; =20 is the same as =20 free(p); =20 in C. =20 Just remember you cannot use one malloc() with delete() and new() with free().... they don't mix. =20 Hope this helps, and anyone else feel free to point out any mistakes I may have made. =20 Jason. ----- Original Message -----=20 From: Joseph, <mailto:bl...@ho...> Brandi, & Elise VanPelt=20 To: dev...@li...=20 Sent: Saturday, September 15, 2001 11:38 PM Subject: [Dev-C++] Hello Hello all, This is my first post to this list. My name is Joseph, you can call me Blumojo (or "blu" or "mojo" for short). I really like Dev-cpp and am thinking of sending a donation if I can get the following to work. I am working on NeHe's OpenGL tutorials (http://nehe.gamedev.net/) which are really good - even for a novice like me, but the code he gave was for Visual CPP. I have worked through a couple of the problems, but have become "stumped" on lesson 7 where the compiler has given me: "implicit declaration of function int free()" In his explanation, this function is used to free memory used to store texture data. =20 First of all, has anyone worked through these tutorials and ported them to Dev-CPP/MinGW? If so, I would greatly appreciate the code to help me along. Second, is there another (perhaps better) way to free this memory? =20 Thank you all so much for the help! =20 -Bumojo |