What the point of Malloc() and Free()
What''s the point of all of these things?
I know they free and delete memory,
but how does this help?
and how would i use it?
Sorry, i was tought C++ using only tutorials so i have holes in my learing
well they are used in dynamic memory allocation.
malloc () is used to give a function, variable ... a certain amount of memory at runtime. meaning the memory is allocated as the program is running not before.
free () just takes the memory that was allocated and (you guessed it) frees it.
for a MUCH better explanation look at www.gametutorials.com they have working code of how this works.
malloc () is used to give a function, variable ... a certain amount of memory at runtime. meaning the memory is allocated as the program is running not before.
free () just takes the memory that was allocated and (you guessed it) frees it.
for a MUCH better explanation look at www.gametutorials.com they have working code of how this works.
THANK YOU!
now i can create my new game in which you must free memory from the clutches of bad sectors!
now i can create my new game in which you must free memory from the clutches of bad sectors!
quote:Original post by MetaKnight
i was tought C++ using only tutorials
Danger danger Will Robinson! Get a book, preferrably a good one. Tutorials just dont cut it when it comes to C++.
When I look upon seamen, men of physical science, and philosophers, man is the wisest of all beings. When I look upon priests, prophets, and interpreters of dreams, nothing is so contemptible as man.
Diogenes
Allow me to throw in my two cents, dynamic memory management is a minefield on which I threaded sucessfully, and I would hate for someone else to have the difficulties I had.
Metaknight, listen up! Instead of Malloc and Free, learn to use new and delete (They''re the C++ equivalent). The principle is as follows: You need to dynamically allocate a class object (Let''s say you have an unknown number of objects to load from a file) you ready a pointer to the class, use ''pointer = new class;'' use the pointer as you will and then ''delete pointer'' when you''re done with it. (You can ''new'' structs and standard types too)
failure to use delete on a new''d object will result in a memory leak. To put it simply, memory leaks are -bad-.
If you need to dynamically allocate an -array-, let''s say an as-yet-unknown number of Vector3''s, simply ready a pointer to the type, and then use ''pointer = new Vector3[number]''. To free that memory, you have to use ''delete [] pointer''.
Here''s a silly code example I wrote off the top of my head.
There, now you had the basic lowdown on new and delete. Notice how I nulled my pointers? It''s good form in any program that any unused pointer be set to null, and delete handles nulls without a hitch (it does nothing with them).
When you''ve started to work with new and delete, you''ll need to learn how to hunt down pesky memory leaks. I suggest you have a look at this site: http://www.FluidStudios.com/publications.html
More specifically download and use MMGR, it''s an awesome add-on to any project that will log your news and deletes, and warn you via log and asserts when you got memory leak. Powerful and easy to use (Just include the .cpp in your project and #include the mmgr.h early on - if after standard C++ includes)
Hope this''ll help you through the treacherous ground you''re about to venture into.
=^.^=
Metaknight, listen up! Instead of Malloc and Free, learn to use new and delete (They''re the C++ equivalent). The principle is as follows: You need to dynamically allocate a class object (Let''s say you have an unknown number of objects to load from a file) you ready a pointer to the class, use ''pointer = new class;'' use the pointer as you will and then ''delete pointer'' when you''re done with it. (You can ''new'' structs and standard types too)
failure to use delete on a new''d object will result in a memory leak. To put it simply, memory leaks are -bad-.
If you need to dynamically allocate an -array-, let''s say an as-yet-unknown number of Vector3''s, simply ready a pointer to the type, and then use ''pointer = new Vector3[number]''. To free that memory, you have to use ''delete [] pointer''.
Here''s a silly code example I wrote off the top of my head.
//dynamic loading example - C++Fct(){int *pInteger;int *pArrayIntegers;// use code here that will fill up ''N'', the number of ints we need in the array// ...pInteger= new int;pArrayIntegers= new int[N];// Code that uses our dynamic data//...// Time for memory cleanup.delete pInteger;pInteger=NULL;delete [] pArrayIntegers;pArrayIntegers=NULL;}
There, now you had the basic lowdown on new and delete. Notice how I nulled my pointers? It''s good form in any program that any unused pointer be set to null, and delete handles nulls without a hitch (it does nothing with them).
When you''ve started to work with new and delete, you''ll need to learn how to hunt down pesky memory leaks. I suggest you have a look at this site: http://www.FluidStudios.com/publications.html
More specifically download and use MMGR, it''s an awesome add-on to any project that will log your news and deletes, and warn you via log and asserts when you got memory leak. Powerful and easy to use (Just include the .cpp in your project and #include the mmgr.h early on - if after standard C++ includes)
Hope this''ll help you through the treacherous ground you''re about to venture into.
=^.^=
That''s one way to say it. Local variables and global variables are put on the stack, and are ''deleted'' when they go out of scope.
Cédric
Cédric
quote:Original post by NeXius
So any variables that aren't declared using new are automatically deleted??
Variables declared on the stack (ie. something like "int a;") are automatically deleted when they go out of scope (past the {} block they were declared in).
Variables declared on the heap (malloc() or new) stay in memory until they are deleted with a respective free() or delete. Therefore:
int main(){ { int a = 5; int *b = new int(3); cout << a; // ok cout << *b; // ok } cout << a; //invalid, a went out of scope cout << b; //ok delete b; cout << b; //invalid, b was deleted return 0;}
[edited by - Neosmyle on September 12, 2002 7:29:07 PM]
[edited by - Neosmyle on September 12, 2002 7:30:09 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement