• Advertisement

Archived

This topic is now archived and is closed to further replies.

Allocating Memory

This topic is 6344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As I have said in earlier posts I''m using C++ for Dummies to learn C++. I have gotten to a part of the book that I don''t understand in the least. This part is about the keywords New and Delete. Now the author expects you to know some C, and I do know SOME C but not very much. He doesn''t explain memory Allocation very well expecting the reader to know about Malloc() and free() but I don''t know that much about C. I wanted to ask if anyone here would help me with my problem.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
It would help if you were more specific (which part of new/delete don''t you understand?)

malloc () returns a pointer to a block of free memory (ie. safe to use, not used anywhere else). free () deallocates that block of memory, so it''s available for use elsewhere. new/delete are similar.

btw, what the hell does that eyeball mean?
MrSmiles121

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It would help if you were more specific (which part of new/delete don''t you understand?)

malloc () returns a pointer to a block of free memory (ie. safe to use, not used anywhere else). free () deallocates that block of memory, so it''s available for use elsewhere. new/delete are similar.

btw, what the hell does that eyeball mean?
MrSmiles121

Share this post


Link to post
Share on other sites
To know about malloc() and free() you'll first have to know about pointers. I'll try to explain malloc() to the best of my knowledge. If anyone sees any mistakes in my explanation then feel free to correct me.

Lets say you have an integer variable,

int myvar;

You can then create a pointer to that variable by first doing this,

int *myvarpointer;

Now you have an integer variable called myvar, and an integer pointer called myvarpointer. To have myvarpointer point to myvar, you do this,

myvarpointer = &myvar;

So now, if you want to do anything to myvar, you can either use myvar, or *myvarpointer, e.g,

myvar = 12;
*myvarpointer = 12;


Both those do the exact same thing, assign myvar a value of 12. You can also do this,

*myvarpointer = 7;

With that, myvar is now 7.

Now lets say you have a pointer called newpointer,

int *newpointer;

It doesn't point to anything, its by itself. Now later on in your program you find out that you need a place to hold 5 integers, but at the beginning of your program you didn't create 5 int variables, or an array or anything, just that pointer. Well now you can do this,

newpointer = malloc(5);

Now your pointer can hold 5 integers, and it can work like an array, so you can do this,

newpointer[0] = 5;
newpointer[1] = 3;
newpointer[2] = 6;
newpointer[3] = 57;
newpointer[4] = 1;


You don't need to put a * in front of it when you want to put your data in there like you do with a pointer that points to a variable that you created at the beginning of your program. When your done with that information you can do this,

free(newpointer);

and it will get rid of the space for all 5 integers, then you can do malloc() again and get space for just 2 integers, or 22, or whatever you need. Just make sure you free() before you malloc() again, and free() at the end of your program, or you'll get a memory leak.

------------
- outRider -

Edited by - outRider on September 3, 2000 10:25:26 AM

Share this post


Link to post
Share on other sites
Noooooooooooo!

Just had to express that feeling towards outRider there, since he did ask to point out any mistakes

OK, well I''ll try to re-iterate and get everything correct.

In C, you can have a type of variable called a pointer variable.
These variables do not contain a value, they contain an address into memory.
A pointer variable is declared by preceding it''s name with an asterisk, like so:

int *my_int_pointer;

Now, you can declare pointers of any type (standard or defined by you). In the statement shown above a pointer is simply declared, in this state a pointer may not be used. To use it one of the following must be done.

Either pointing the pointer to an object already allocated, as shown below.

int my_int;

my_int_pointer = &my_int;

Now, the & operator retrieves the address of the variable it precedes, and this is what we want, because pointers store addresses.

The alternative would be to dynamically allocate memory for the pointer, this would be done something like this.

my_int_pointer = (int *)malloc(sizeof(int));

malloc() allocates the number of bytes specified in it''s parameter, and you want the number of bytes an integer takes up, so you use sizeof(int). You must cast the pointer to that of what you are using, because malloc() normally returns a void *.
Note: error checking excluded in previous example.

Anyway, if you chose to dynamically alloctate memory, when you are done you should free it, to avoid the much hated ''memory leaks''. This is acheived with a call to free(), as shown below.

free(my_int_pointer);

remember, you DO NOT need to free pointers that were referencing another already allocated variable.

In C++, malloc and free are achieved with the operators new and delete.

You should never mix the two conventions, i.e. don''t new something and then free() it.

The advantages of the new/delete operators are that new automatically returns a pointer of the correct type (no (int *) casting) and it automatically allocates the correct amount of space given a parameter.

int *my_int_pointer = new(int);

I think that''s how it works, and you can then call delete as such.

delete(my_int_pointer);

I''m not too certain about the C++ conventions, but I hope I was able to explain the C variety enough for you to get a good grip on things.

-Mezz

Share this post


Link to post
Share on other sites
Actually, there is no need to cast malloc, and doing so can cause an error with the right (or wrong) conditions.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

Share this post


Link to post
Share on other sites
Why would you use sizeof() to get the size of an int? Doesn''t the compiler know the size of an int on the system already? Or does malloc() always return bytes? I thought that sizeof() was needed only when you weren''t using native types like int or short or whatever, but I''ll remember to get the size of each type before I malloc from now on.

------------
- outRider -

Share this post


Link to post
Share on other sites
Thanks, I understand it now, it all makes sense, well most of it anyways.
I find you people on Gamedev.net are better at explaining things that my damn book.
Oh and by the way, I already understood pointers, I should have said something but I didn''t know that pointers had anything to do with New/free

Share this post


Link to post
Share on other sites
Just one last thing on new/delete. Not only do you not have to do the type-casting, and sizeof() stuff, but it also knows to run your class constructors and destructors.

If you have a C++ class called "MyClass" and you want to create a new one you can do:

MyClass *tmp = new MyClass;

However, if you do this with malloc() you will get the correct memory size, but malloc will not call the default constructor for your class. This can be bad if you have implementation for a default constructor.

That also goes for ''delete'' as it calls your destructor.

Just my 4.5 cents.

CodeMonkey

Share this post


Link to post
Share on other sites
outRider: Using sizeof(int) instead of hard-coding the value will ensure that if you were to compile the same C code on two different platforms, it will remain correct. For example, if you wrote the code for a DOS compiler, and just used 2 bytes as the size of an int, if you were to compile and run that code as a Win32 console app, you''d only allocate half as much memory as you need.

-Ironblayde
 Aeon Software

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
www.bruceeckel.com

Share this post


Link to post
Share on other sites

  • Advertisement