Crazy memory problem with arrays

This is a discussion on Crazy memory problem with arrays within the C++ Programming forums, part of the General Programming Boards category; Hey, folks. I've got a weird problem with a bunch of arrays I'm allocating on the free store. Before I ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    35

    Crazy memory problem with arrays

    Hey, folks.

    I've got a weird problem with a bunch of arrays I'm allocating on the free store.

    Before I post any code, let me just explain the general situation and maybe someone can tell me if I'm on the right track.

    Basic idea:
    Allocate arrays of objects to be used for characters etc. during a game. Each level needs its own arrays for the characters etc. of that level.

    So, when you advance a level,
    a) old arrays are deleted
    b) new arrays are established.

    Also, when you start a new game (because you messed up or something and just want to start over) the arrays are deleted and everything is set up again.

    I'm at the point in developing the game where I can't move forward until I get the level-advance mechanism working.

    So here's the problem.

    1). I declare one-dimensional arrays on the free store and use them during the course of the game.

    2) When the level's over (or, right now, when I choose to start over) I delete the arrays using delete[].

    First observation: My compiler, which is either the Cygwn or Mingw compiler under Dev-C++ 4, issues a warning that I'm deleting these arrays, like so:

    506 c:/program files/gmmain.cpp
    warning: deleting array `class Junction * pJunctionArray[(gNumJunctions + 1)]'

    Now, I don't see why I should get warnings here since delete[] is a perfectly ordinary process, no?

    And here's the abyss of the problem itself.

    I have the game set up so when you close it (click the X) you get a dialog box which gives some options, one of which is to restart the game. So, to test out this deallocate/reallocate business I sat there and kept restarting the game over and over. I won't get into why I already suspected there'd be a problem , but I'm glad I kept hitting the restart button because it's only after the 20th or even 30th restart that the program finally does what the Titanic does best, and plunges inexplicably into the depths.

    What's going on here!??? I thought that maybe the deallocate wasn't working and I was running out of memory, so I added a routine to catch an out-of-memory error, but that turned up nothing. So what's going wrong?

    If the game successfully restarts 30 times, why not 31 times? Why not 1031 times? Eh eh eh??? What is hap-hap-happenning in the hidden gremlins of my computer?

    My delete[] should work!

    If I declare an array of objects on the free store, and then delete them, can't I then use that same pointer to an array and fill it up with new objects?

    Do I have to set that pointer (those pointers?) to null before I use it again, or what?

    Bottom line: How do I put an array of objects on the free store, get rid of them, and then put them back?

    I'll put up some code if you don't get what I'm saying, just tell me what you want to see.

    I really need to sort this out to build new levels in my game! The frustrating thing is it almost works! If I sort this out the rest is easy sailing.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    it sounds to me like you aren't using new to allocate your array. In fact, it sounds like your array is either global or on the stack, in which case you shouldn't be deleting it. That's why you're getting a warning -- using an array name by itself returns a pointer to the first element, which is valid SYNTAX for using delete, but it doesn't give you the right to delete it. Only delete stuff that you "new."

  3. #3
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    801
    Poly, quick question, where does dynamically allocated data get stored?

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The heap.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Aha, Polymorphic, you may have it.

    The pointer to the array is not on the heap, you're right. Each element is on the heap. In other words I'm doing this:

    Code:
    class *arrayOfObjects[number];
    arrayOfObjects[n] = new class(etc);
    So basically the pointer to the array is indeed on the stack and that's why I can't delete it.

    So I've got to delete each member of the array individually, because each member is on the heap.

    It's 3am here so I'm thanking you before I try it out, but it sounds right to me.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The ARRAY is on the stack -- there isn't really a pointer to the array anywhere except temporarily when the array name is used by itself (if you want to look at it like that). Also, when you delete something, it doesn't matter where the pointer is stored, it's where the data pointed to is stored. The pointers usually ARE on the stack. But yeah, you must delete each element individually -- only use delete [] if the array itself is what's dynamically allocated.

    *Thinks about writing an article on arrays and pointers. A lot of people seem to have the same problem with the topic*
    Last edited by Polymorphic OOP; 01-14-2003 at 09:48 PM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    What is the "array" that's on the stack, if the data is on the free store?

    What exactly is the difference, in terms of where pointers are stored, between an array allocated dynamically and an array whose members are individually allocated? It would seem to me that it's this:

    In the first method the 1 pointer on the stack points directly to the first member of an array on the free store and that's it.

    In the second method there is a pointer on the stack for each member of the array. So if 500 objects are on the free store, there are 500 pointers to objects pointing to them on the stack (this is what I've done).


    Correct?

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by fusikon
    What is the "array" that's on the stack, if the data is on the free store?
    All of the elements of the array (the pointers to what's on the heap).


    Originally posted by fusikon
    What exactly is the difference, in terms of where pointers are stored, between an array allocated dynamically and an array whose members are individually allocated? It would seem to me that it's this:

    In the first method the 1 pointer on the stack points directly to the first member of an array on the free store and that's it.

    In the second method there is a pointer on the stack for each member of the array. So if 500 objects are on the free store, there are 500 pointers to objects pointing to them on the stack (this is what I've done).


    Correct?
    Yup.
    Last edited by Polymorphic OOP; 01-14-2003 at 10:52 PM.

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well i am not sure how you are restarting the game.. But hope this helps..

    I created a snake(nibbles) game whcih could be restarted by the user.. But say after 20 restarts the game used to crash, close etc... The bug was that though i was deleting the mess etc.. I was virtually in a way calling the program again and again like a recursive function...... So the instance of the previous session or game was still alive...


    THis might be the problem.. Just a guess....

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    35
    Nope, my restart is just based on a while loop.

    The problem was what we suspected: the arrays weren't getting deleted.

    I made a console driver for creating and deleting the arrays of my game and it was pretty clear. In the console application I was able to keep reallocating different array sizes to make sure all was smooth.

    Yep, it works all right. Adding new levels will be a breeze now. Thanks for help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 07:22 AM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  5. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21