Thread: Class and pointer to pointer

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Class and pointer to pointer

    Hi!

    I have a few questions about ** (pointer to pointer).

    Let's say I have a class named Activity.

    I would code it like this ...
    Code:
    int tableLength = 3; // 2 from before and 1 new
    Activity **newTable = new Activity*[tableLength]; // 3 activities in newTable => is this correct?
    // now add old activities into a new table
    for (int i = 0; i < tableLength - 1; i++)
    {
      Activity *act = getActivity(i); // getActivity(i) returns Activity*
      memmove(newTable[i], act, sizeof(Activity*)); // is this correct?
      // is this correct =>> newTable[i] = act;?
      delete []act; // is this correct?
      act = NULL;
    }
    // and add a new one
    memmove(newTable[tableLength - 1], act, sizeof(Activity*)); // is this correct?
    // some code where I use the newTable
    
    // and than free - is this correct?
    for (int i = 0; i < tableLength; i++)
    {
      delete []newTable[i];
      newTable[i] = NULL;
    }
    delete []newTable;
    newTable = NULL;
    So, please take a look at the code and tell me what am I doing wrong.

    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Sweden
    Posts
    16
    The = operator should work just fine. Which delete operator to use depends on which new operator you allocated with, ie if you allocated with new [], then free with delete [].

    The only obvious error I can see after a quick look is that you assign the value of a freed 'act' to 'newTable[tableLength - 1]', right under the first for loop. I'm not sure what it is you want to do there, but I'm quite certain it does not involve assigning a pointer you're going to use to invalid memory...

    [edit] Another thing, if you use the assignment operator, you should not delete the 'act' pointer, because the 'newTable[something]' pointer will be pointing to the same memory, which will be freed when you free the table, anyaway.[/edit]
    Last edited by Kenki; 05-26-2005 at 05:25 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    Activity **newTable = new Activity*[tableLength]; // 3 activities in newTable => is this correct?
    I may just be a semanic quibble, but no it is not right. The only thing in newTable is the address in memory where an array is located. newTable is said "to point" to that array. The array contains 3 Activity pointers, which means that each of them can be made to point to an Activity object (or an array of Activity objects).

    Code:
    Activity *act = getActivity(i); // getActivity(i) returns Activity*
    Is getActivity() a function? Or, is it a method of a class? If it's a method of a class it has to be called using an object of that class--unless it is a static method, which means you would call it with the class name.

    Code:
    memmove(newTable[i], act, sizeof(Activity*)); // is this correct?
    Couldn't say...I've never heard of a memmove() function, which means if it exists, it's probably a C function.

    Code:
    // is this correct =>> newTable[i] = act;?
    Yes, assuming act is of type Activity*. newTable is a pointer to an array of Activity pointers, and you can use array notation with a pointer to an array to access the elements of the array. That means:

    newTable[0]
    newTable[1]
    newTable[2]

    are all of type Activity* and can be assigned an Activity*.

    Code:
    delete []act; // is this correct?
    It doesn't look like it, but you can't tell from the code whether the pointer assigned to 'act' points to memory that was dynamically allocated with new. The brackets are for deleting a pointer that points to an array. If instead, a pointer points to a single element, then you just use delete.

    If you have a pointer to an array of pointers, and each of the pointers in the array points to dynamically allocated memory, first use a for loop to delete the memory pointed to by each of the pointers in the array using delete. Then, after the loop has finished, delete the pointer to the array using delete [].
    Last edited by 7stud; 05-26-2005 at 05:09 PM.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thanks for the replies!

    "The array contains 3 Activity pointers ..."
    That's what I meant. So that's cool.

    Code:
    for (int i = 0; i < tableLength - 1; i++)
    {
      Activity *act = someClass->getActivity(i);
      newTable[i] = act;
      delete []act;
      act = NULL;
    }
    In this method "getActivity" I use new function. So I have to free that act variable after I'm done with her, right? Or the someClass destructor deals with that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by GaPe
    Code:
    for (int i = 0; i < tableLength - 1; i++)
    {
      Activity *act = someClass->getActivity(i);
      newTable[i] = act;
      delete []act;
      act = NULL;
    }
    In this method "getActivity" I use new function. So I have to free that act variable after I'm done with her, right? Or the someClass destructor deals with that?
    This is wrong because if you free the memory that act points to, you are also freeing the same memory that you just assigned to newTable[i]. When you try to use newTable later it will cause problems.

    I would recomend doing this:
    Code:
    for (int i = 0; i < tableLength; i++)
    {
      newTable[i] = getActivity(i); //have getActivity() return a pointer to dynamically allocated Activity 
      //or it could even return a pointer to a dynamically allocated array
    }
    Now when you are done with that memory, you can do this.
    Code:
    for (int i = 0; i < tableLength; i++)
    {
      delete newTable[i];
    }
    
    delete [] newTable;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  3. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM