Jump to content
  • Advertisement

Archived

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

Tornado

Can I use new more than once

This topic is 6616 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

Can I use the new operator more than once ? For example:
    
int *somearray;
for (int x=0;x < 5;++x)
{
  somearray = new int[x];

}
    
Would this work and make an array of int[4] ? Thanks The road to success is always under construction

Share this post


Link to post
Share on other sites
Advertisement
Don''t do that. If you want an array int[4] you would do this :

int *pointer = new int[4];

Nate Miller
http://nate.scuzzy.net

Share this post


Link to post
Share on other sites
Yes, it''s going to make an array of 4 ints (i.e. int[4]), but you''re also going to lose 6*4=24 bytes of memory (in Windows) if you use that code. The thing is that you allocate memory, and before you have freed it, you allocate another piece of memory, and throw the old pointer to the allocated memory in the trash, and now there''s no way to pick it up and deallocate that memory. So, the best thing is not to use THAT code AS IT IS. You can for example create a 2D array, by doing something SIMILAR to what you''re doing, but not quite the same as yours.

-------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
No,

When you declare a pointer like this : int *ptr_int;

What you really create is a variable, which store the adresse of an ''int'' (16 bits long).

So it''s for that reason, that you can do :

ptr_int = new int [10];

with this, "ptr_int" will have the adress of the first block of the new array of 10 (also called vector).

But by the way you do it, that will create 5 array of (x).

I mean :

1st : int [0]
2st : int [1]
3th : int [2]
4th : int [3]
5th : int [4]

and your pointer "somearray" will take the adress of the last array assign, so it''s will have the adress of int[4].

All the previously allocated array will be lost, (THAT IS A LEAK). So be warn dont do this. If you wanna to have a stable program.

It''s seem that Windoze will find and kill those lost allocated memory block in it''s spare time, after your program has stopped.

But, i dunno if you''re like me, but never thrust a O.S., and a lot less when it''s Windows.

Happy Coding

LowRad

Share this post


Link to post
Share on other sites
Thanks for your help guys
I''m working on my level editor and I wanted the editor to be able to load any bitmap and find how many tiles there are in it.
So I started out by doing a "sfTiles = new LPDIRECTD..." everytime I found a new tile, My game froze everytime.
So now it does 2 loops instead of one - The first figures out how many tiles in the bitmap, right afterwards I create the surfaces with a new and the second loop loads the tiles to their surfaces.
Thanks



The road to success is always under construction

Share this post


Link to post
Share on other sites
Couldn''t you just use realloc() or its CPP equivalent to change the size of the memory block that the pointer is pointing to if you need more storage? Or is there a better way to do it? realloc() isn''t used very much according to a few books, so I''m guessing theres a reason for that.

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

Share this post


Link to post
Share on other sites
quote:

There''s a C++ equivalent?
Anyone?



std::vector.resize(), but there''s no realloc type thing. It''s not safe to move objects around in memory, so with C++ you need to make a copy then destroy the old one. That''s the way std::vector does it if it needs to move its contents when you call resize.

Share this post


Link to post
Share on other sites
quote:
and your pointer "somearray" will take the adress of the last array assign, so it's will have the adress of int[4].


WRONG! It points to the first element.
Quote from MSDN:
quote:
When new is used to allocate a singly dimensioned array of objects, it yields a pointer to the first element of the array, and the resultant type is new-type-name * or type-name *.


The correct syntax is:
int *somearray = new int[x];  


The new operator returns a pointer to the allocated memory, so you need a pointer variable to hold the location (supported by the above MSDN quote). I have no idea what just a regular variable would do, but it ain't good.

Edited by - Zipster on September 2, 2000 7:34:22 PM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!