Debug Assertion Error

This is a discussion on Debug Assertion Error within the C++ Programming forums, part of the General Programming Boards category; Try it out. Looks good. Note, you have only allocated memory for 5 of the possible 10 elements in Anthony.Location. ...

  1. #16
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try it out. Looks good.


    Note, you have only allocated memory for 5 of the possible 10 elements in Anthony.Location. If you try doing this:

    Anthony.Location[5][1].setDescpription("BOO!");

    your program may well behave erratically or, if you're lucky, crash. To fix this potential problem, look more closely at Salem's code.
    Last edited by elad; 11-21-2004 at 04:12 PM.

  2. #17
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    OHHHHHHHHHHHHHHH. I SEE THE ERROR NOW ) LOL I accidently put the loop for 5 not 10
    THANKS

  3. #18
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I believe it should be like this:

    Code:
    Anthony.Location = new Space*[10]; 
    for( int i=0;i<10;i++){
        Anthony.Location[i]=new Space[5];
    }
    //...
    for( int i=0;i<10;i++){
    delete [] Anthony.Location[i];
    }
    delete [] Anthony.Location;
    Also, your class design isn't all that great. Any data in a class should generally be declared with private scope, and then you should declare a constructor and destructor to allocate/deallocate memory:
    Code:
    class Player
    {
    public:
    Player(int r=10,int c=5);
    ~Player();
    private:
    Space** Location;
    int rows,cols;
    };
    Player::Player(int r, int c)
    {
      rows=r;
      cols=c;
      Anthony.Location = new Space*[r]; 
      for( int i=0;i<r;i++){
          Anthony.Location[i]=new Space[c];
      }
    
    }
    Player::~Player()
    {
      for(int i=0;i<rows;i++){
        delete [] Anthony.Location[i];
      }
      delete [] Anthony.Location[i];
    }
    You should probably review a tutorial on classes before you move on

    Edit: Whoops missed the [] in the second part
    Last edited by JaWiB; 11-21-2004 at 03:58 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #19
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    I Know my Class isn't that Great. Im Just Tring to get the Parts working before I start organizing it better. But then again Im just a beginner. So It wasnt going to get much more better then it was without help LOL. Thanks for the Help. The Correct way to Delete really Helped. Also I would change your code a tad. So that Space handles all the Memory Allocations and stuff. And Addings new levels. I liked the new additions with the constructors. I need to learn more on classes I agree. However I think Coding and learning as I go is helping me a whole lot. Im not a real good learner when it comes to reading. And then It is hard for me to remember the things I learn on my way LOL. Thanks for all the good advice.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM

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