Menu

array of pointers to objects

2003-02-12
2012-09-26
  • Nobody/Anonymous

    i am trying to create an array of objects
    is the following the "best" way to do it?

    typedef Box* Boxes;
    Box** myBoxes = new Boxes[20];
    .
    .
    for (int i=0; i<20; i++)
    delete myBoxes[i];
    delete [] myBoxes;

    bl

     
    • Curtis Sutter

      Curtis Sutter - 2003-02-12

      That way creates a array of pointers to your class.
      Try this instead...

      Box* myBoxes = new Box[20];
      .
      .
      for (int i=0; i<20; i++)
           delete myBoxes[i];
      delete myBoxes;

      Curtis

       
    • Nobody/Anonymous

      Curtis want to know if i understand. iam not   bl, just read your post and want to learn too.,thanks.

      Box* myBoxes = new Box[20];

      myBoxes a pointer to type Box, assigned  the "space (using 'new' )" for an array 'Box[20]'  that the  array 'Box[20'   elements are each of type Box also.

      The pointer 'myBoxes' has only one element: 'Box[20]'  so in order to access an element of 'Box[20]'  would be something like:  myBoxes.Box[0], that would be the first element of 'Box[20]'  ?.

      lj

       
    • Curtis Sutter

      Curtis Sutter - 2003-02-12

      typedef Box* Boxes; // Allows Boxes to be used instead of Box* in line below.
      Box** myBoxes = new Boxes[20]; // Creates a array of pointers to type Box
      .
      .
      for (int i=0; i<20; i++) // Loops 20 times (0-19)
      delete myBoxes[i]; // deletes memory referenced at that location
      delete [] myBoxes; // deletes the array of pointers created on 2nd line

      Issues with this method...
      1)  I personally don't use typedef, but that is personal, not wrong
      2)  Creating pointers to a type does not really make it a type.  Change "Box** myBoxes = new Boxes[20];"  to "Box* myBoxes = new Box[20];" or "Boxes myBoxes = new Box[20];"
      3)  With the point above, using the delete cmd will cause a error, since it wil try to delete the pointer that points to nothing (junk) values.

      Since I am doing this totally from memory, if anyone notices a mistake on my part, do let me know.  Wouldn't want to teach someone something wrong.  :-)

      I hope this explains it well enough, if not, ask a more specific question for me to try and answer.

      Curtis

       
    • Nobody/Anonymous

      thanks for the feedback.
      i appreciate what you are saying, but is there another way to crate an array of pointers?

       
    • Curtis Sutter

      Curtis Sutter - 2003-02-14

      int *Hello[10]; // array od int pointers

      Try to keep in mind, that without using the 'new' keyword, you are creating your variable in your program 'stack'.  The new function puts them into the 'freestore'.  For a comparsion, think of the 'stack' as a floppy disk, and the 'freestore' would then be a DVD.  Your program is also put into the stack, so the less you have there, the better.  But useing the stack is easier than the freestore.  If you do not use the delete the memory created by the new cmd, expect a possible memory leak.

      // Start of Memory leak Example
      int main()
      {
        char *temp;
        while(1)
           temp = new char;
        return 0;
      }
      // End of Memory leak example

      // Same Idea, no memory leak...
      int main()
      {
        while(1)
           char temp;
        return 0;
      }
      // End example

      Both programs compile on Dev 4.9.7.5 / gcc 3.92  Win XP.  The second gives a expected unused variable warning.  o compensate for the memory leak in the first example, you must use the delete cmd.  Like so:

      // Delete cmd example
      int main()
      {
        char *temp;
        while(1)
        {
          temp = new char;
          delete temp;
        }
        return 0;
      }
      // End Example

      *As a side note, the memory usage of the 1st program is lower than the 3rd.

      Curtis

       
    • Nobody/Anonymous

      hey Curt... question... you ever had a free problem with arrays at odd intervals? just curious... I have seen it in a few things... though it has been extremely rare... here is what i mean... (paracode)

      somestruct ** something = new somestruct * [amount]
      while(somenum <= amount)
      something[somenum] = new somestruct
      do stuff
      while(somenum <= amount)
      free(something[somenum]) or delete something[somenum]

      i have actually done this and regardless of what the 3rd([2]) structure was used for or even if it was used... it would kill the program... and I mean... if i created exactly 3 when freeing the memory it would cause a fault of somesort... create 2 it would run fine reate 4 or more (to the extent i tried) would work as well... just wondering if you had ever seen anything like it... I have even seen it doing absolutely nothing with any of the memory except for deleting it... oh well... one more odd quirk ive seen...

      Zero Valintine

       
    • Curtis Sutter

      Curtis Sutter - 2003-02-14

      I have never used the free() function myself.  So I can't relate.  I will look into the power of '3', haven't noticed it before, but will watch for it in the future.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.