Pointer Arrays

This is a discussion on Pointer Arrays within the C++ Programming forums, part of the General Programming Boards category; Hey, I was wondering if this method of using an array should be avoided: Code: class Get { int itsNum; ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Pointer Arrays

    Hey,

    I was wondering if this method of using an array should be avoided:

    Code:
    class Get
    {
      int itsNum;
    
    public:
      Get(int num) : itsNum(num) {}
    
      int GetNum() { return itsNum; }
    
    };
    
    int main()
    {
      Get* Array[500];
      int i;
    
      for(i = 0; i < 500; i++) {
        Array[i] = new Get(i);
      }
    
      for(i = 0; i < 500; i++) {
        cout << Array[i]->GetNum();
      }
    
      for(i = 0; i < 500; i++) {
        delete Array[i];
      }
      
      cin.get();
    }
    The reason I ask is because the tutorials and book I've read dont even consider to mention this. Usually indirect methods are taken, for example creating another pointer to the class, then allocating the space for that class, then pointing 1 of the pointers of the original array of pointers to it. For example:

    Code:
    5:     class CAT
    6:     {
    7:        public:
    8:           CAT() { itsAge = 1; itsWeight=5; }   
    9:           ~CAT() {}                                 // destructor
    10:          int GetAge() const { return itsAge; }
    12:          void SetAge(int age) { itsAge = age; }
    13:
    14:       private:
    15:          int itsAge;
    17:    };
    18:
    19:    int main()
    20:    {
    21:       CAT * Family[500];
    22:       int i;
    23:       CAT * pCat;
    24:       for (i = 0; i < 500; i++)
    25:       {
    26:          pCat = new CAT;
    27:          pCat->SetAge(2*i +1);
    28:          Family[i] = pCat; 
    29:       }
    30:
    31:       for (i = 0; i < 500; i++)
    32:       {
    33:          cout << "Cat #" << i+1 << ": ";
    34:          cout << Family[i]->GetAge() << endl;
    35:       }
    36:     return 0;
    37: }
    Code:
    5:     class CAT
    6:     {
    7:        public:
    8:           CAT() { itsAge = 1; itsWeight=5; } 
    9:           ~CAT();         
    10:          int GetAge() const { return itsAge; }
    12:          void SetAge(int age) { itsAge = age; }
    13:
    14:       private:
    15:          int itsAge;
    17:    };
    18:
    19:    CAT :: ~CAT()
    20:    {
    21:      // cout << "Destructor called!\n";
    22:    }
    23:
    24:    int main()
    25:    {
    26:       CAT * Family = new CAT[500];
    27:       int i;
    28:       CAT * pCat;
    29:       for (i = 0; i < 500; i++)
    30:       {
    31:          pCat = new CAT;
    32:          pCat->SetAge(2*i +1);
    33:          Family[i] = *pCat;
    34:          delete pCat;
    35:       }
    36:
    37:       for (i = 0; i < 500; i++)
    38:       {
    38:          cout << "Cat #" << i+1 << ": ";
    39:          cout << Family[i].GetAge() << endl;
    40:       }
    41:
    42:       delete [] Family;
    43:
    44:     return 0;
    45: }
    See for that second example why use:

    Code:
    26:          pCat = new CAT;
    27:          pCat->SetAge(2*i +1);
    28:          Family[i] = pCat;
    instead of:

    Code:
    26:          Family[i] = new CAT; 
    27:          Family[i]->SetAge(2*i +1);
    The only reason I can see is so that you can simply delete [], instead of using a delete array like I used. The same thing goes for for the second example, and you can simply delete the array # instead of the copy.

    Or for that first example could I use this instead?:

    Code:
      int i = 0;
      Get* Array = new Get[500](i++);
    
      for(i = 0; i < 500; i++) {
        cout << "\n" << Array[i].GetNum();
      }
    
      delete[] Array;
    Last edited by Dae; 07-05-2005 at 05:21 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They're in effect the same thing. I believe it would be the same as if you had done:
    Code:
    for( pCat = Family; pCat < Family + 500; pCat++ )
    {
        pCat = new CAT;
        pCat->SetAge( (Family + 500) - pCat) * 2 + 1 ); /* I think that math is right. ;) */
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Oh cool, I'm not sure why this eBook uses that extra pCat (= new CAT) then assign the pointer to it, instead of directly defining it (=new CAT).

    Nice method there Quzah, saved yourself from using 4 bytes (int i)
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 04:45 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. pointer arrays!!!
    By condorx in forum C Programming
    Replies: 1
    Last Post: 05-14-2002, 09:55 AM
  4. pointer arrays
    By condorx in forum C Programming
    Replies: 3
    Last Post: 05-03-2002, 10:04 PM
  5. Replies: 4
    Last Post: 11-05-2001, 02:35 PM

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