473,378 Members | 1,400 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,378 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 2258
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.