|
From: Jason H. <jas...@bt...> - 2001-09-16 12:04:50
|
MessageThanks, I wrote about it of the top of my head, but not using = dynamic allocation of arrays all that often I forgot about the array = version. Only in recent weeks have I started coding again, so I'll = admitt I am a bit rusty, but it's just like riding a bike :) Any more mistakes anyone? Jason. ----- Original Message -----=20 From: Ioannis Vranos=20 To: dev...@li...=20 Sent: Sunday, September 16, 2001 4:03 AM Subject: RE: [Dev-C++] Hello I write this because you said to point out the mistakes. :) It is not new() and delete(), they are operators new and delete ( and = new[] and delete[]). Then in your code you do p=3Dnew int[10] and delete p; but it should = be delete[] p; 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. Regards, Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * 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()". 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(). 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. 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. i.e. int *p; p =3D new int[10]; // create an int array for(int i=3D0; i<10; ++i) p[i] =3D i*i; // fill array with square of 0 through to 9. 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... delete p; // always free up memory or you will get a memory leak!!! ... 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. p =3D new int[10]; is the same as p =3D (int*) malloc(10*sizeof(int)); in C. delete p; is the same as free(p); in C. Just remember you cannot use one malloc() with delete() and new() = with free().... they don't mix. Hope this helps, and anyone else feel free to point out any mistakes = I may have made. Jason. ----- Original Message -----=20 From: Joseph, 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. 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? Thank you all so much for the help! -Bumojo |