Aayush Kumar
Aayush Kumar

Reputation: 13

How to create an Instance of a class on heap memory

A class type called "Pair" has already been defined for you. You need to write a function called pairFactory that creates an instance of Pair on the heap. Do not create the object on the stack. Then, your function needs to return a pointer to that created object.

I have written the code for pairFactory. It seems to run, but I get an InfraError. Please help me find my mistake. Also, I need to create the object in heap memory.

#include <iostream>

// This class Pair has already been defined for you.
// (You may not change this definition.)
class Pair {
public:
  int first, second;
  void check() {
    first = 5;
    std::cout << "Congratulations! The check() method of the Pair class \n has executed. (But, this isn't enough to guarantee \n that your code is correct.)" << std::endl;
  }
};

Pair *pairFactory() {
  //
    Pair P;
    P.check();
  // (You can use as many lines as you want.)
  return &P;
}

// Your function should be able to satisfy the tests below. You should try
// some other things to convince yourself. If you have a bug in this problem,
// the usual symptom is that after you submit, the grader will crash with a
// system error. :-)
int main() {
    Pair *p;
    p = new pairFactory();

  // This function call should work without crashing:
  p->check();

  // Deallocating the heap memory. (Assuming it was made on the heap!)
  delete p;

  std::cout << "If you can see this text, the system hasn't crashed yet!" << std::endl;

  return 0;
}

Upvotes: 1

Views: 5413

Answers (5)

Tribhuvan
Tribhuvan

Reputation: 1

The issue with your code is that it returns a pointer that points to nothing as P only exists till the value of p is returned, as stored in heap memory. Using new keyword will allocate the memory in heap and will return a pointer to the value.

 Pair *pairFactory() {
 Pair P;
 P.check();
return &P;
}

Upvotes: 0

Sheiks Beer
Sheiks Beer

Reputation: 1

The function

Pair *pairFactory() {
    return &P;
}

returns a pointer to memory on the local stack, which is destroyed / invalid as soon as it returns to main().

Upvotes: 0

Jarod42
Jarod42

Reputation: 217255

You return reference to local variable here.

Pair *pairFactory() {
    Pair P;
    P.check();
  return &P; // Dangling pointer
}

So you have dangling pointer, once you leave the function.

You have to call new.

Pair *pairFactory()
{
  return new Pair{};
}

main may look like:

int main() {
  Pair* p = pairFactory();

  // This function call should work without crashing:
  p->check();

  // Deallocating the heap memory. (Assuming it was made on the heap!)
  delete p;

  std::cout << "If you can see this text, the system hasn't crashed yet!" << std::endl;
}

Better to use smart pointer to not have to manage memory yourself:

std::unique_ptr<Pair> pairFactory()
{
    return std::make_unique<Pair>();
}

int main() {
    auto p = pairFactory();

    p->check();
    std::cout << "If you can see this text, the system hasn't crashed yet!" << std::endl;
}

Upvotes: 3

datenwolf
datenwolf

Reputation: 162164

You did exactly as you were told not to do:

Pair *pairFactory() {
    Pair P; // <----- creates an instance of Pair on the stack
    …
}

The intention of this exercise likely was to test your knowledge on the new operator. See here https://en.cppreference.com/w/cpp/memory/new/operator_new

Upvotes: 1

sweenish
sweenish

Reputation: 5202

You've got it backwards. Your factory needs to allocate on the heap. What you're doing is returning the address of a function-local object that doesn't exist anymore.

Pair *pairFactory() {
  return new Pair;
}

And then in your main function:

Pair *p = pairFactory();

Upvotes: 5

Related Questions