-6

I am learning classes in c++ so I try different things to get used to the classes. Here I am trying to pass a pointer to a class function:

class Pointerss{
    int size;
public:
    Pointerss(int);
    void f();
    int *a;
};

Pointerss::Pointerss(int siz){
    size = siz;
}

void Pointerss::f(){
    cout<<"Size is:"<<size<<"\n";
    for (int i=0; i<size; ++i) {
       cout<<"a is:"<<a[i]<<"\n";
    }
}
int main() {
    int size = 5;
    Pointerss dd (size);
    Pointerss * p = new Pointerss(size);
    p[0]=1; p[1]=2; p[2]=3; p[3]=4; p[4]=5;
    p->a;
    dd.f();
    return 0;

}

So the "size" is initialized by a constructor and when "f()" is called the correct digit is printed. But when I assign "p" to "a" ("p->a") and then call "f()" some random numbers are printed. My computer prints :

Size is:5
a is:0
a is:0
a is:1
a is:0
a is:1606416856
Program ended with exit code: 0

what is the difference between "a" and "size" and what should I do to pass a pointer to a function?

Amin
  • 261
  • 3
  • 16
  • 1
    I can see at least 3 different questions. – Mohamad Elghawi Dec 01 '15 at 09:31
  • 2
    This is going to be closed and deleted, but you need to read a beginner [C++ book](http://stackoverflow.com/q/388242/96780) that teaches you what "p->a" is (certainly, not an assignment) and how to do variable initializations and assignments. – Daniel Daranas Dec 01 '15 at 09:31
  • 1
    `Pointerss* p = new Pointerss(size)` allocates a single element, not an array. so `p[1]=2` is out of bound access and so is UB. – Jarod42 Dec 01 '15 at 09:35
  • There is also a good tutorial here: http://www.cplusplus.com/doc/tutorial/ – J. Chomel May 27 '16 at 13:36

2 Answers2

1

Random numbers are printed because Pointerss::f dereferences dd.a which you never initialized. Dereferencing an uninitialized pointer results in undefined behaviour.

That's not the only bug you have. You also allocate memory for a single Pointerss object, which is pointed by p, but then proceed to construct objects in p[1..4] which point to unallocated memory. That also results in undefined behaviour.

Thirdly, you never deallocate the memory that you allocated with new.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Step 1 would be to understand pointers. When you make a int* you only reserve memory to store the pointer itself. You don't reserve memory for any int. So when you try to print the integers you just get random numbers (or perhaps a program crash).

So your class can't hold any integers. You need to change it so that you reserve memory in the constructor and release it in the destructor.

class Pointerss{
    int size;
public:
    Pointerss(int);
    ~Pointerss();
    void f();
    int *a;
};

Pointerss::Pointerss(int siz){
    size = siz;
    a = new int[size];  // Reserve memory for size integers

    // Do some initialization if needed, e.g.
    for (int i=0; i < size; i++) a[i] = i;
}

Pointerss::~Pointerss(){
    delete[] a;         // Release the memory again
}

void Pointerss::f(){
    cout<<"Size is:"<<size<<"\n";
    for (int i=0; i<size; ++i) {
       cout<<"a is:"<<a[i]<<"\n";
    }
}

Further you can't make a single pointer to an instance of the class, i.e.

Pointerss * p = new Pointerss(size);

and then use it like

p[0]=1; p[1]=2; p[2]=3; p[3]=4; p[4]=5;

because that is operating on 5 different instances.

Finally the code:

p->a;

does nothing!

You could do

p->a[0] = 5;

but it is unclear what you are trying to do.

Notice that dd and p are refering to two different instances of your class so changing something inside *p doesn't change dd at all.

If you had done:

Pointerss *p = &dd;

then *p and dd would be the same instance.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63