Results 1 to 12 of 12

Thread: deleting class array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89

    deleting class array

    Hi, I've been able to create an array of class variables, but I can't get the program to delete them without an access violation.
    Thanks!
    PHP Code:
    class TEST
    {
      public:
        
    TEST(int y);
        
    int x;
    };

    TEST::TEST(int y)
    {
      
    x=y;
    }

    class 
    TEST2
    {
      public:
        
    TEST *test[];
    };

    void main()
    {
      
    TEST2 t2;
      for(
    int i=0i<6;i++)
        
    t2.test[i] = new(TEST)(i); // creating

      // delete them here



  2. #2
    Member
    Join Date
    Oct 2002
    Location
    Look out your window.
    Posts
    54
    What exactly are you tring to do?


    I guess you would just do something like this:

    Code:
    for(i = 0; i < 6; i++)
        delete t2.test[i];
    Normally class members are destroyed in the classes Destructor... It is automatically called by 'delete'

    Code:
    class TEST
    {
    public:
        TEST(void);
        // This is the destructor.
        ~TEST(void);
    };
    
    TEST::~TEST(void)
    {
        for(int i = 0; i < 6; i++)
            delete this->test[i];
    
    }

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Where the deletion is doesn't matter. And since the array is a member of TEST2 it certainly won't be in the TEST destructor.

    But the core deletion code should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    It still is giving an access violation when it tries to delete the pointer.
    PHP Code:
    TEST2::~TEST2()
    {
      for(
    int i=0i<6;i++)
        
    delete test[i]; 

    I've tried how many different sorts of combinations with delete but whatever I try it keeps having an access violation.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, and did I mention that [] is bad to use here anyway? This array has no lenght. You may not access it's elements because it has none. Declare test as
    TEST *test[6];
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    I can't place
    TEST *test[6];
    because I want it to be a dynamic array, thats whats been giving me problems, I figured you would just start with
    TEST *test[]; but trying to delete it won't work, so I don't know how to have the dynamic array and still delete it

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Then I suggest you don't use an array of pointers but rather one pointer and use array new:
    Code:
    TEST *ar;
    ar = new TEST[6](1);
    // ...
    delete[] ar;
    Note that you can't pass individual arguments to the constructors anymore, only the same to each constructor. But this syntax is far easier to use. Else you'd have to use:
    Code:
    TEST **ar;
    ar = new TEST*[6];
    for(int i=0;i<6;++i)
      ar[i] = new TEST(i);
    // Access:
    ar[3]->DoSomething();
    // Deletion:
    for(int i=0;i<6;++i)
      delete ar[i];
    delete[] ar;
    Quite bad, and terribly prone to memory leaks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    what I've meant is instead of allocating 6 I want that to be a variable that gets increased everytime I call a function of mine. I've been able to get it to work with a fixed amount to begin with, but not when its a variable

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you can allocate a variable amount of objects with new
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But if that amount changes you have to reallocate the memory every time. In that case you should rather use a vector:
    Code:
    #include <vector>
    using std::vector;
    
    typedef vector<TEST> testvec;
    
    testvec array;
    // add a new element:
    array.push_back(TEST(4));
    No deletion needed, the vector does it all for you.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    that works much better thank you! is there any tutorial that shows more about the vector, like how to go about shrinking the vector array and swapping values

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't know if there is a tutorial for vectors, but a good reference should suffice. Look up vector in MSDN or on SGI's reference page.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width