473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2259
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

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

Similar topics

4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
12
by: gc | last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a system of equations f(a,c)=b for a nx1 c. While writing the function: 1] Should I allocate the memory for c within the...
7
by: Michael | last post by:
Hi, What's the benefit to dynamically allocate memory? using namespace std; int main() { char* ptr; ptr="abc";
5
by: ampeloso | last post by:
Hello, I would like to allocte memory, but I want it to start at a predefined address. I have a program that writes data to ROM in an embedded device and I would like to state where it goes. Can...
20
by: ramasubramanian.rahul | last post by:
hi folks i have a peculiar problem. i have to allocate more than size_t consequtive bytes on a system . after i do a malloc .. i am unable to do a realloc because it takes size_t as a new size...
2
by: lovecreatesbea... | last post by:
If the built-in operator keyword new doesn't allocate memory on heap and it calls global operator new (::operator new) or class member operator new to do that. What are the two kinds of operator...
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
9
by: Steven Powers | last post by:
Imagine the following setup class Parent { virtual void doStuff(); } class Child : public Parent { virtual void doStuff(); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.