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?

8
  • 1
    You might be interested in the rule of three. you should also consider using an std::vector<double> instead of an dynamic array. Commented Oct 1, 2013 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]; Commented Oct 1, 2013 at 14:05
  • @juanchopanza: Yeah, that's what I meant. Thanks! Commented Oct 1, 2013 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. Commented Oct 1, 2013 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? Commented Oct 1, 2013 at 14:27

1 Answer 1

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.
};
Sign up to request clarification or add additional context in comments.

5 Comments

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?
@user2609987 Glad it helped. But which part don't you understand?
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?
@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.
Ok, I use vector now. Thanks a lot for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.