-1

I'm having trouble because of a feature of c++. after working with dynamic memory allocation, I always clear the heap(free store) because of obvious reasons. I do it with the destructor function. And sometime, allocate memory using the constructor. but after calling an object from another function(call by value), only the destructor works. I know that it's a good feature in many cases. But, it is annoying while working with a lot of stand-alone functions. Any way around it,guys? Or, should i just use a member function to allocate and clear memory?

An example:

#include <iostream>
using namespace std;

class test{
int *a;
public:
test(int x) {a=new int;
             *a=x;}
int geta(){return *a;}
 ~test(){delete a;}
};

int apow2(test t)
{
return ((t.geta())*(t.geta()));
}

int main()
{
test tst(10);
cout<<"\nA^2="<<apow2(tst);
//at this point, memory reference to 'a' doesn't exist
cout<<"\n"<<tst.geta();
return 0;
}
  • 8
    Pick up a book on C++ and learn the language systematically. Most of what you describe sounds completely confused. – Kerrek SB Apr 10 '15 at 08:06
  • [Perhaps one of these.](http://stackoverflow.com/q/388242/1171191) – BoBTFish Apr 10 '15 at 08:07
  • If you haven't done so yet, please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). You might also want to learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Apr 10 '15 at 08:07
  • 2
    You're missing the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – Mat Apr 10 '15 at 08:37
  • @KerrekSB I am learning c++ systematically. I'm sorry if my description sounded confusing. now that i have read it myself, it really is confusing :P . – user2143094 Apr 10 '15 at 08:42
  • @Mat thanks for referring to it. – user2143094 Apr 10 '15 at 08:44
  • 1
    @user2143094 Pretend that `new` never existed. Now rewrite your class given that assumption. See how easy it becomes now? The point being is that you introduced `new` in one of the most inappropriate places -- all you require is an `int` member, not an `int *` member. – PaulMcKenzie Apr 10 '15 at 09:06

1 Answers1

1

You're having trouble because you did not implement the copy constructor and copy assignment operator for class test: you did not abide by the rule of three.

But, good news! You don't need to. You can avoid all of this kerfuffle by storing an int instead of an int*:

class test
{
   int a;

public:
   test(int x)
     : a{x}
   {}
};

That's it! No dynamic allocation, no memory management, nothing. Just test, and an int that is assured to survive for precisely test's lifetime. Perfect.

If you need dynamic allocation for reasons not stated in your question (whoops), then you should use a smart pointer to manage its lifetime for you:

class test
{
   std::unique_ptr<int> a;

public:
   test(int x)
     : a{new int(x)}
   {}

   int geta() { return *a; }
};

That's it! No memory management, nothing. Just test, and an int* whose pointee is assured to survive for precisely test's lifetime. Perfect.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055