Can a class has a member array which will be dynamically allocated during runtime?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runn
    New Member
    • Aug 2008
    • 4

    Can a class has a member array which will be dynamically allocated during runtime?

    Can a class has a member array which will be dynamically allocated during runtime? Such as

    double* A=new double[n]

    here n is an integer member variable and will be determined during runtime and the class will have the array *A allocated.

    Thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Yes

    You might normally allocate the memory for the array in the constructor or some other member function of the class and then delete it in the destructor.

    Comment

    • runn
      New Member
      • Aug 2008
      • 4

      #3
      The code is below:

      Code:
      #include <iostream>
      using namespace std;
      
      class MyClass
      {
            public:
            int n;
            double* A;  // One of the problem is the declaration I think ...
            MyClass(int);
            virtual ~MyClass();
            void Print();
      };
      
      
      MyClass::MyClass(int _n)     
      {
           n=_n;
           double* A=new double[n];
           if (!A) abort();
           A[0]=0.0;A[1]=10.0;A[2]=20.0;A[3]=30.0;
           cout <<A[0]<<endl;
           cout <<A[1]<<endl;
           cout <<A[2]<<endl;
           cout <<A[3]<<endl;
           cout <<""<<endl;
      }
      
      MyClass::~MyClass()     
      {
           delete []A;
      }
      
      void MyClass::Print()
      {
           cout <<A[0]<<endl;
           cout <<A[1]<<endl;
           cout <<A[2]<<endl;
           cout <<A[3]<<endl;
      }
      
      
      int main ()
      {
       MyClass m(4);
       m.Print();
       return 0;
      }
      and the output of this code is:
      • 0
        10
        20
        30

        1.98218e-317
        1.17289e-046
        0
        1.94765e-308



      First 4 output comes from the constructor. Considering the last 4 output, I understand that allocated memory is local to the constructor member function.

      Another problem is the declaration of A I think...

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        That is because in the constructor rather than using the member variable A you declare a local variable A on the stack that is then lost when the function exits producing a memory leak.

        Nothing is ever assigned to the member variable A.

        Comment

        • runn
          New Member
          • Aug 2008
          • 4

          #5
          If I use the A in the member function in which the memory is allocated there is no problem. Every thing is OK. However I want to use it outside the member function also. For this purpose, When I make an declaration as

          double* A[n]

          compiler says:
          invalid use of non-static data member `MyClass::n'

          If I make n is static, there will be only one n while there are several MyClass objects. Each of them needs a n variable. Or I am wrong, not sure ...

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            May I ask why you are not just using a vector?

            Comment

            • arnaudk
              Contributor
              • Sep 2007
              • 425

              #7
              Originally posted by runn
              However I want to use it outside the member function also.
              So then why are you making a local redeclaraion (see reply #4)? Remove "double* " from line 18. Even better, use an initializer list:
              [code=cpp]
              MyClass::MyClas s(int _n) : A(new int[_n])
              {
              n=_n;
              if (!A) abort();
              A[0]=0.0;A[1]=10.0;A[2]=20.0;A[3]=30.0;
              // etc
              }
              [/code]

              Comment

              • runn
                New Member
                • Aug 2008
                • 4

                #8
                Originally posted by Banfa
                May I ask why you are not just using a vector?
                Thank you for answers. I am new to C++ and want to learn its basic rules at the beginning. Actually I want to write a matrix class and use it for scientific computing. They say vector class is not fast especially for small matrices. There may be a class from blitz which is fast. I can use it, but later.

                Originally posted by arnaudk
                So then why are you making a local redeclaraion (see reply #4)? Remove "double* " from line 18. Even better, use an initializer list
                Because I didnt know how to allocate memory from inside the constructor. Thank you for pointing out initializer list. The code works now.

                Comment

                Working...