2

I have a class with a dynamic array (DA)

class DA{
  private:
    double* array;
    int size N;
    //other stuff
  public:
    DA(){
       array=NULL;
    }
    DA(int PN){
      N=PN;
      array=new double[N];
    };
    //destructor and other stuff
}

This seems to be ok. Now I want a class "Application" that has one DA object:

class App{
  private:
    DA myDA;
  public:
    App(int N){
      //create myDA with array of size N
      DA temp(N);
      myDA=temp;
    };
}

The problem is, that I don't know how to create myDA in the App constructor. The way I do it, memory is allocated for temp, then myDA points to temp. But I guess the memory allocated for temp is deleted after the constructor finishes. Hence, I get a memory error when I execute my program. So how do I allocate memory correctly?

thomasfermi
  • 1,183
  • 1
  • 10
  • 20
  • 1
    You might be interested in [the rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). you should also consider using an `std::vector` instead of an dynamic array. – andre Oct 01 '13 at 14:02
  • 2
    Note that `array = new double(N);` is creating a pointer to a single `double`. I assume you meant `array = new double[N];` – juanchopanza Oct 01 '13 at 14:05
  • @juanchopanza: Yeah, that's what I meant. Thanks! – thomasfermi Oct 01 '13 at 14:11
  • 1
    Classes should have as few responsibilities as is sane. Memory management is a huge, huge responsibility in itself, opening up class authoring issues (Rule of Three), exception safety issues (virtually impossible to come by correctly by C++ beginners _and_ intermediates), increasing code complexity, introducing hardcoded dependencies on certain data structures, and some more. Better just skip (prefer value types) or outsource memory management to smart pointers and (even better) standard containers. – Sebastian Mach Oct 01 '13 at 14:13
  • @phresnel: The reason I don't want to use a container like vector, because I fear performance decrease. The array member of DA should contain something like 10^6 doubles. Wouldn't those containers be a problem then? – thomasfermi Oct 01 '13 at 14:27
  • @user2609987, so you prefer buggy code that leaks memory to an imaginary performance decrease? Stay the hell away from my code. – Jonathan Wakely Oct 01 '13 at 14:30
  • @user2609987: No. Your other problems outweigh your (non-) performance problem by universes; your code leaks, corrupts, and provokes UB. Manual memory management is only for highly educated C++ masters (in contrast to bachelors), and even they will probably make errors. It is easier in C, but in C++, you have exceptions and copyable classes. The implication thereof makes manual memory management in C++ a horrible pain. And finally: No, there is no performance penalty. At least not as long as you haven't profiled your code, and reads/writes to `std::vector` probably not even after profiling – Sebastian Mach Oct 01 '13 at 14:36
  • Thanks for the comments! I will try to avoid manual memory management from now on (will use vector in this specific program). I'm a beginner, so I didn't know that this can cause serious trouble. – thomasfermi Oct 02 '13 at 09:31

1 Answers1

7

Using a constructor initialization list:

App(int N) : myDA(N) {}

Note that your DA class is broken unless you follow the rule of three, or simplify the problem by using an std::vector<double>, an std::unique_ptr<double[]> or a boost scoped array:

#include <vector>

class DA{
  private:
    std::vector<double> data; // "array" is a std lib container name
  public:
    DA(int PN) : data(PN) {}
    // no need to write destructor, copy constructor or assignment operator.
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks a lot for your help! This works:) But I'm not sure if I understand it: I should now always create "App" objects by App(N). Then automatically one "DA" object with an N-dimensional array is created. Is this correct? – thomasfermi Oct 01 '13 at 14:12
  • @user2609987 Glad it helped. But which part don't you understand? – juanchopanza Oct 01 '13 at 14:13
  • My class "DA" will be quite heavy later on, with N=10^6 or something like that. Won't this array solution be faster than using vector? If "DA" is created once I don't want to change the size of array, so I don't need vector, do I? Regarding "RuleOf3": I would now need a destructor and copy constructor in order to ensure that not only the pointer to array is copied/destructed but the whole array is. If I implement this, it will be ok? – thomasfermi Oct 01 '13 at 14:20
  • @user2609987 You should not get any overhead from using a `vector` rather than a dynamically allocated array. Your class will be a small number of bytes larger, but that shouldn't really matter. Concerning the rule of three, you would have to implement an copy assignment operator too. Or disallow copying and assignment for your class, if that suits the design better. – juanchopanza Oct 01 '13 at 14:34
  • Ok, I use vector now. Thanks a lot for the help! – thomasfermi Oct 02 '13 at 09:27