• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

C++ When to use pointers?

Status
Not open for further replies.

Digital

Learning C++
Joined
Jul 15, 2018
Messages
57
Solutions
1
Reaction score
12
I am learning C++ and wondered when its appropriate to use pointers on an object.

So here is my example.
C++:
#include <iostream>

class A {
    virtual void call() = 0;
};

class B : public A {
    public :
    void call(){
        std::cout << "This is class B" << std::endl;
    }
};

class C : public A {
    public :
    void call(){
        std::cout << "This is class C" << std::endl;
    }
};

int main(){
    B b;
    C c;
    C* d;

    b.call();
    c.call();
    d->call()
    return 0;
}

If I try to run this program as it is then it doesn't display anything on the screen but if I comment out d->call() then it displays.
Code:
This is class B
This is class C
 
Solution
I'm definitely not an expert in c++ and it is a complex language. But I will make an attempt to answer.

Creating a normal object will put it on the stack and creating an object with "new" will allocate it on the heap.

Stack
  • The stack is the memory that is set aside for a thread before it executes
  • The size of the stack is fixed and if you try to use more than there is available you will get a stack overflow error
  • Objects created on the stack will automatically be deleted as soon as they go out of scope

Heap
  • The heap is the memory that is set aside for dynamic allocation for your application to use
  • The heap grows as you request more memory (allocate more objects with "new")
  • Allocating objects on the heap...
You are missing a semicolon after d->call()

The way you instantiate an object in c++ is:
Code:
C* d = new C;
 
You are missing a semicolon after d->call()

The way you instantiate an object in c++ is:
Code:
C* d = new C;
I missed the semi-colon because I didn't copy and paste the code I wrote it freehand. So now I am able to access the object's method using a pointer but you really haven't answered my question about when its appropriate and whats the difference between a pointer & non pointer objects and how is one better over the other?
 
I'm definitely not an expert in c++ and it is a complex language. But I will make an attempt to answer.

Creating a normal object will put it on the stack and creating an object with "new" will allocate it on the heap.

Stack
  • The stack is the memory that is set aside for a thread before it executes
  • The size of the stack is fixed and if you try to use more than there is available you will get a stack overflow error
  • Objects created on the stack will automatically be deleted as soon as they go out of scope

Heap
  • The heap is the memory that is set aside for dynamic allocation for your application to use
  • The heap grows as you request more memory (allocate more objects with "new")
  • Allocating objects on the heap is slower compared to creating them on the stack
  • The objects created on the heap will stay there until you delete them
  • When programmers by mistake forget to delete objects that are allocated on the heap it will cause a memory leak

You should always prefer to create objects on the stack because it is less error-prone. And since c++ also allows passing objects by reference allocating on the heap is often not needed.

Example:
Code:
#include <iostream>

class B {
    public :
    int testVariable;
    void call(){
        std::cout << "This is class B" << std::endl;
    }
};

void passByValue(B b1) {
  b1.testVariable = 2; //This is just a copy of b1 so changeing it will not affect the original b1.
} //This copy of the object will automatically be deleted when it goes out of scope

void passPointer(B* b2) {
  b2->testVariable = 2; //We got the pointer that is pointing at the original object in the heap. We can therefore change its value.
}

void passByReference(B &b3) { //Takes a reference
  b3.testVariable = 2; //We got the reference to the object so we can change it.
}

int main(){

  //Passing an object to a function
  B b1 = B(); //Creates object on the stack
  b1.testVariable = 1;
  passByValue(b1); //Passing the object to this function will copy the object
  std::cout << "Value of b1 is: " << b1.testVariable << std::endl; //So the value will remain the same

  //Passing a pointer to a function
  B* b2 = new B(); //Creates object on the heap
  b2->testVariable = 1;
  passPointer(b2); //Passing the pointer to this function will just make a copy of the pointer
  std::cout << "Value of b2 is: " << b2->testVariable << std::endl;
  delete b2; //We are done using b2. We should delete it.


  //C++ also allows passing by reference!

  //Passing a reference to a function
  B b3 = B(); //Creates object on the stack
  b3.testVariable = 1;
  passByReference(b3); //The function takes a reference so here we are actually just passing by reference.
  std::cout << "Value of b3 is: " << b3.testVariable << std::endl;

  return 0;

} //The objects on the stack are deleted when they go out of scope

Output:
Code:
Value of b1 is: 1
Value of b2 is: 2
Value of b3 is: 2

For information on when to use pointers: stackoverflow.com/why-should-i-use-pointers
 
Last edited:
Solution
Status
Not open for further replies.
Back
Top