Did I Use Delete Correctly?

This is a discussion on Did I Use Delete Correctly? within the C++ Programming forums, part of the General Programming Boards category; Hello, I been working on my text game again and unsure if I used Delete Correctly. Someone Suggested I use: ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    49

    Did I Use Delete Correctly?

    Hello, I been working on my text game again and unsure if I used Delete Correctly. Someone Suggested I use:
    Code:
    Player::~Player()
    {
      for(int i=0;i<rows;i++){
        delete [] SpaceName[i];
      }
      delete [] SpaceName[i];
    }
    But it didnt work. Here is my Code.
    Code:
    #pragma once
    #include "Player.h"
    class Space{
    public:
    void SetDescription(string);
    string GetDescription();
    Space();
    ~Space();
    void CreateSpace(int,int);
    private:
    string Description;
    Space **SpaceName;
    int rol;
    int col;
    };
     Space::Space(){};
    void Space::CreateSpace(int rol,int col){
    	this->rol=rol;
    	this->col=col;
     SpaceName = new Space*[rol]; 
      for( int i=0;i<rol;i++){
        SpaceName[i]=new Space[col];
      }
    
    };
    Space::~Space(){
    for( int i=0;i<rol;i++){
    delete [] SpaceName[i];
    }
    for(int x=0;x<col;x++){
    delete[] SpaceName[x];
    }
    }
    void Space::SetDescription(string Description){
    this->Description=Description;
    }
    string Space::GetDescription(){
    	return Description;
    }
    Thank you for taking time to look at my problem.
    Byebye From me. Have a Nice Day

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Code:
    Player::~Player()
    {
        for(int i=0;i<rows;i++){
            delete [] SpaceName[i];
        }
        delete [] SpaceName;  // No [ i ] here
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Thank you, Hk.
    That wasnt the error after all. It had to do with me tring to put the Memory allocation and deallocation stuff within Space. Everytime Space is Constructed and destructed it calls the memory stuff and cuases errors so it seems. So Ill have to find a way to make space be apart of another class which can construct and destruct the memory. Otherwise Ill be lefted with.
    Code:
    #pragma once
    #include "Player.h"
    class Space{
    public:
    void SetDescription(string);
    string GetDescription();
    Space();
    ~Space();
    void CreateSpace(int,int);
    void DeleteSpace();
    private:
    string Description;
    Space **SpaceName;
    int rol;
    int col;
    };
     Space::Space(){};
    Space::~Space(){
      }
    void Space::DeleteSpace(){
      for( int i=0;i<rol;i++)
      {
        delete [] SpaceName[i];
      }
    
      delete [] SpaceName;
    }
    void Space::CreateSpace(int rol,int col){
    	this->rol=rol;
    	this->col=col;
     SpaceName = new Space*[rol]; 
      for( int i=0;i<rol;i++){
        SpaceName[i]=new Space[col];
      }
    };
    void Space::SetDescription(string Description){
    this->Description=Description;
    }
    string Space::GetDescription(){
    	return Description;
    }
    And I dont want to have to call DeleteSpace method and CreateSpace Method. IT Defeats the Purpose of the Class. So I think my answer lies in linked Lists. How they have the Linked List class handle the Node class. But that Is maybe too Advance for me. Unsure. Thanks for the Help.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It may be you just need to redisign the class some--nothing unusual in that. Can you describe why you want to have a pointer to pointer to type Space as data member in the Space class?

    Code:
    class Space
    { 
     private:
    	Space **SpaceName
    };
    Pointer to type as members are commonly used to create linked lists or trees, but I've not seen a pointer to pointer to type as class member before.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    49
    Hehe, Us bad programmers that werent made to be programming, Will do many things that you never seen done before lOLLLL.
    I wanted the, Pointer to Pointer, Of type space so I can create the Space Dynamic Multi-Dimensional array. And you need a Pointer to Pointer to allocate Memory I believe. I wanted to Make it apart of Space as a Data member cause I wanted to keep all the space related Data and members together.
    Later on. I was going to assign a Player class' Location data member which will be a Space ***Location; LOL to the SpaceName Pointer. Then Ill beable to move my player through the Multi-dimensional array. The Space Description will display the description for the space he lands on. That was the plan anyways. But it seems Like if I make the Space a node and Made a Linked list to handle Space. Ill have more control on how I build the spaces in the rooms I made.
    Im unsure if this is a good way, But I think its a unique way of making a Text Game like a 3d game. Well. Hope that answers your Question. Im just not a programmer type. 4 years and cant make a text game is fustrating LOl. but I never study hard and stuff and off and on so. Well Maybe you can comment on my idea nad give me tips and stuff. Thanks.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    The new design sounds better to me. I'd think of a Rubicks cube as a concrete visual analagy, where the big cube is made of smaller cubes. In your case maybe you would have Space be the small cubes and Universe be the bigger cube. Of course, there is no big deal about cubes to describe a unit of Space as opposed to some other container which may be brick like or cube like or whatever. If each unit of Space are all the same size/shape and each unit abuts another on each edge then a mulitdimensional array would be a convenient way to visualize the Universe. Alternatively, if each unit of space is different size/shape or is non-contiguous, then a list/tree may be a better way to visualize the Universe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 03:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 05:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 10:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM

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