New and Delete
Hi, I cant quite figure out the point in dynamically creating variables using the new keyword. I mean what difference doesi t make if i create an int like int number = 0; or new int number = 0;
One is allocated within the .exe file (space is reserved), while when using new, it allocates the memory when the function scope is reached. I beleive that''s the only major difference.
new/delete are more used for dynamic sized arrays and stuff.
new/delete are more used for dynamic sized arrays and stuff.
February 10, 2003 10:23 AM
Well if you do:
int var;
your application is going to take up 4 bytes for the entire time it runs and that 4 bytes is not reusable.
if you do:
int *var = new int;
then your application will take up 4 bytes until you delete it. This way it''s reusable.
Dynamic memory allocation is useful when you don''t know the sizes of things like images. Is it 640x480 or 1280x1024 in size? If you don''t know then you need to make a huge array and waste space.
Dynamic memory is a way of using the memory available more effectively than just using plain old static memory
int var;
your application is going to take up 4 bytes for the entire time it runs and that 4 bytes is not reusable.
if you do:
int *var = new int;
then your application will take up 4 bytes until you delete it. This way it''s reusable.
Dynamic memory allocation is useful when you don''t know the sizes of things like images. Is it 640x480 or 1280x1024 in size? If you don''t know then you need to make a huge array and waste space.
Dynamic memory is a way of using the memory available more effectively than just using plain old static memory
February 10, 2003 10:24 AM
new is useful for situations in which it is not known how much memory will be needed before the program actually runs. A good example of this is strings. Suppose all of your strings are statically allocated character arrays. There will be times when you will run out of space when trying to write to a string. And there will also be times when you are only using a small fraction of the allocated space, and are therefore wasting memory.
Dynamic memory is useful in such situations because the amount of allocated memory can be adjusted depending on how much is needed. There are many other uses, especially in data structures (linked lists, binary trees, hash tables, etc.)
Of course, though this is one of the most useful language features, it is also the most dangerous. A large proportion of programming errors are caused by the misuse of dynamic memory.
-D
Dynamic memory is useful in such situations because the amount of allocated memory can be adjusted depending on how much is needed. There are many other uses, especially in data structures (linked lists, binary trees, hash tables, etc.)
Of course, though this is one of the most useful language features, it is also the most dangerous. A large proportion of programming errors are caused by the misuse of dynamic memory.
-D
It''s needed for things like dynamic arrays, linked lists or trees, and using polymorphism.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
As you continue to learn C++ you will see the importance of dynamically allocating memory.
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement