how to allocate dynamic memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diwakar09
    New Member
    • Sep 2007
    • 39

    how to allocate dynamic memory

    in case of dynamic memory allocation
    with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor.

    please help me out
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by diwakar09
    in case of dynamic memory allocation
    with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor.

    please help me out
    Please read memory allocation fundamentals from some c++ book or from cplusplus.com or related c++ site. Also read constructor destructor. Follow posting guidelines of TSDN.

    Regards

    Comment

    • diwakar09
      New Member
      • Sep 2007
      • 39

      #3
      [QUOTE=zodilla58]Please read memory allocation fundamentals from some c++ book or from cplusplus.com or related c++ site. Also read constructor destructor. Follow posting guidelines of TSDN.



      with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor.
      i know the concept of concept of constructor and destructor
      as we use for loop
      in constructor
      Code:
      for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
      in destructor how to dealloacte this memory
      please help me out

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by diwakar09
        with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor.
        i know the concept of concept of constructor and destructor
        as we use for loop
        in constructor
        Code:
        for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
        in destructor how to dealloacte this memory
        please help me out
        In destructor,
        Code:
        for (i=0;i<10;i++)  delete a[i];
        Regards

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          [QUOTE=diwakar09]
          Originally posted by zodilla58
          Please read memory allocation fundamentals from some c++ book or from cplusplus.com or related c++ site. Also read constructor destructor. Follow posting guidelines of TSDN.



          with the use of new operator i have to allocate memory of integer array of 10 elements in constructor and similarly deallocate it in destructor.
          i know the concept of concept of constructor and destructor
          as we use for loop
          in constructor
          Code:
          for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
          in destructor how to dealloacte this memory
          please help me out
          Actally, you would allocate it like this if you just want an array of integers.:
          [code=cpp]
          int *myArray = new int[10];
          [/code]
          To deallocate:
          [code=cpp]
          delete[] myArray;
          [/code]
          If you want an array of pointers to integers allocated on the heap, then that is different. Something like:
          [code=cpp]
          int *myParray[10];
          for (int x = 0; x < 10; x++)
          myParray[x] = new int;
          // ... to deallocate ...
          for (int x = 0; x < 10; x++)
          delete myParray[x];
          [/code]

          Comment

          Working...