C ++ pointer arithmetic and classes

So, I've just started learning pointer arithmetic and I've been working with some of its capabilities. As soon as I started trying to cheat with pointer and class arithmetic, I ran into a problem. I wrote the following code below:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class Cat
{
public:
    Cat();
    ~Cat();
    int GetAge() { return itsAge; }
    void SetAge(int age) { itsAge = age; }

private:
    int itsAge;

};

Cat::Cat()
{
}

Cat::~Cat()
{
}

int _tmain(int argc, _TCHAR* argv[])
{

    Cat *Family = new Cat[5];
    Family = Family + 1;
    Family->SetAge(3);
    cout << Family[1].GetAge()<< endl;

    return 0;
}

      

In my opinion, I am creating a pointer called "Family" that will point to an array of Cat objects. This pointer will represent the address of the family [0]. Then, on the next line, I have a Family pointer pointing to the new address by adding 1 to the pointer itself (so the compiler has to take this as moving the address slot to the next element in the array, Family [1]). Then I set the age to 3 and try to output the family age value [1], however the answer I get is 842150451, not 3. What am I missing?

+3


source to share


5 answers


itsAge is not initialized because you did not set it in the default constructor. It is currently trash.

Cat::Cat()
: itsAge(0)
{
}

      



This becomes a problem because Family [1] points to Cat after you've initialized. Pointer [1] is equivalent to * (Pointer + 1).

+6


source


I see a couple of questions:



  • itsAge

    is not initialized in the class constructor. Change it to:

    Cat::Cat() : itsAge(0)
    {
    }
    
          

  • Your understanding of pointer arithmetic is slightly flawed.

    You have:

    Cat *Family = new Cat[5];   // Family points to the first Cat
    Family = Family + 1;        // Now Family points to the second Cat
    Family->SetAge(3);          // Set the age of the second Cat
    
    cout << Family[1].GetAge()<< endl; // Since Family points to the
                                       // second object, Family[0] is the
                                       // second object. Family[1] is the third
                                       // object, not the second object.
                                       // That is the misunderstanding
    
          

+4


source


Note that when you grow a family,

Family = Family + 1;

      

Family

indicates the location corresponding Cat[1]

. Now you set the age Cat[1]

using:

Family->SetAge(3);

      

But in the following expression, you get a value from the [1] family, which actually points toCat[2]

:

cout << Family[1].GetAge()<< endl;

      

So it prints garbage as it is Family[1]

equivalent *(Family+1)

, i.e. increasing it again.

You can use Family-> GetAge () instead :

Cat *Family = new Cat[5];   
Family = Family + 1; 
Family->SetAge(3);
cout << Family->GetAge()<< endl;

      

Also keep in the habit of using delete

dynamic allocations to prevent memory leaks .

+2


source


Try:

Cat *Family = new Cat[5];
(Family + 1)->SetAge(3);
cout << Family[1].GetAge()<< endl;

      

+1


source


Please see the comments in the code below:

#include <iostream>
#include <string>
using namespace std;

class Cat
{
public:
    Cat();
    ~Cat();
    int GetAge() { return itsAge; }
    void SetAge(int age) { itsAge = age; }

private:
    int itsAge;
};

Cat::Cat() : itsAge(0)
{
}

Cat::~Cat()
{
}

int main(int argc, char* argv[])
{

    Cat *cats = new Cat[5];
    for( int i = 0; i < 5; ++i )
    {
      cats[i].SetAge( i + 1 );
    }

    //Here cats points to the first cat so, it will print 2
    cout << cats[1].GetAge()<< endl;

    //Now cat will be pointing to second cat as pointer will be moved forward by one
    cats = cats + 1;

    //below statement will print 3, as cats[0] is pointing to 2nd cat, and cats[1] will be pointing to 3rd cat
    cout << cats[1].GetAge()<< endl;

    return 0;
}

      

+1


source







All Articles