470,815 Members | 1,203 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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
Sep 24 '07 #1
4 2018
Meetee
931 Expert Mod 512MB
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
Sep 24 '07 #2
[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
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
  3.  
  4.  
in destructor how to dealloacte this memory
please help me out
Sep 24 '07 #3
Meetee
931 Expert Mod 512MB
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
  3.  
  4.  
in destructor how to dealloacte this memory
please help me out
In destructor,
Expand|Select|Wrap|Line Numbers
  1. for (i=0;i<10;i++)  delete a[i];
  2.  
Regards
Sep 24 '07 #4
ilikepython
844 Expert 512MB
[quote=diwakar09]
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. for (i=0;i<10;i++) &a[i]=new(int); //some thing like that i am not sure whether it is correct or not
  3.  
  4.  
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.:
Expand|Select|Wrap|Line Numbers
  1. int *myArray = new int[10];
  2.  
To deallocate:
Expand|Select|Wrap|Line Numbers
  1. delete[] myArray;
  2.  
If you want an array of pointers to integers allocated on the heap, then that is different. Something like:
Expand|Select|Wrap|Line Numbers
  1. int *myParray[10];
  2. for (int x = 0; x < 10; x++)
  3.     myParray[x] = new int;
  4. // ... to deallocate ...
  5. for (int x = 0; x < 10; x++)
  6.     delete myParray[x];
  7.  
Sep 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Franklin Lee | last post: by
5 posts views Thread by lixiaoyao | last post: by
12 posts views Thread by gc | last post: by
5 posts views Thread by ampeloso | last post: by
20 posts views Thread by ramasubramanian.rahul | last post: by
2 posts views Thread by lovecreatesbea... | last post: by
11 posts views Thread by Bryan Parkoff | last post: by
9 posts views Thread by Steven Powers | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.