Thread: dynamic reallocation

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    dynamic reallocation

    Ok, I'm working on a game with several guns that shoot bullets. Each gun class has a dynamic array of bullet objects (I know it should be a linked list) which is resized each time a new bullet is created with this function:
    Code:
    void gun_t::addbullet(float setx, float sety, float setth) {
      int i;
      bullet_t *temp = new bullet_t[numbullets];
      for (i = 0; i < numbullets; ++i)
        temp[i] = bullet[i];
      delete [] bullet;
      ++numbullets;
      bullet = new bullet_t[numbullets];
      for (i = 0; i < numbullets; ++i)
        bullet[i] = temp[i];
      bullet[numbullets-1].set(setx,sety,setth);
      delete [] temp;
    }
    So I set up the call to this function like this:
    Code:
      int temp = numbullets;
      addbullet(x,y,th);
      if (temp == numbullets) error("what?");
    This error message shouldn't be displayed, but it is. Does anybody know why this is happening?
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, just look at what you are doing

    Code:
      int temp = numbullets;
      addbullet(x,y,th);
      if (temp == numbullets) error("what?");
    You don't pass temp into the function so it is not going to be modified. Of course it will still be equal to numbullets after the function call. You need to get the new number of bullets after the function call and then compare.

    P.S. - All those allocations are going to be super slow if you are shooting a lot of bullets. You should look into making a memory manager to avoid all those small allocations. Or look into boost's library which provides a fast memory manager for just such things ( bullets, particles, etc. )

    www.boost.org
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    i'm assuming this is a class, so the fact that he used a temp variable is correct. The numbullets variable is the one that SHOULD be changing, but it's not.

    I'd suggest popping some error messages in to the addbullet function and making sure that it is actually called, also, put some errors at the front and the end of the function that output the numbullets variable, so you can see if it's being reset somewhere else for some reason

    -edit-

    oh yah, i'd look in to the realloc function for resizing your array of bullets, not only is it faster, but it's a lot cleaner too :d

    http://nehe.gamedev.net/data/article...asp?article=05

    look down near the bottom and look at the realloc function, if you've never used it before

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > which is resized each time a new bullet is created with this function:
    It would be far more efficient to either allocate for the max number of bullets, or allocate them in blocks of say 5 or 10 at a time.

    > I know it should be a linked list
    Yeah, because when you come to removing bullets, you'll see that they don't necessarily impact in the order in which you fired them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    one other way you could do it is to have a "peak" bullets, and as each bullet dies, just fill its spot in the array with a NULL pointer or something. Then, when you create a new bullet, just search for the first open slot in the list and pop the new bullet in there. If you don't find a new slot, then resize the array. This basically makes it so that over time the size of the array averages out pretty nicely. It would also be best to initially size the array to an amount that seems reasonable for your game, so you won't have to resize it unless a LOT of things are flying around.

    But in the end, a linked list would of course function much better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reallocation
    By George2 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2008, 08:13 PM
  2. Identify dynamic IP address of network device
    By BobS0327 in forum Tech Board
    Replies: 2
    Last Post: 02-21-2006, 01:49 PM
  3. Dynamic memory reallocation
    By Gravedigga in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2005, 06:39 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 11:48 AM