Link to home
Start Free TrialLog in
Avatar of arindam042099
arindam042099

asked on

Dynamic Allocation

I am using to new operator to dynamically allocate memory for a multi-dimensional array ... What all other types of methods in dynamic allocation can I use besides the new operator ??? I am reading the data from a file which will store the content in the array and then use it later for computation ... The length of the array depends on the number of buses (power system) in the data file ... I was (with the new operator) reading the data file initially to find the number of buses and then using that number of memory allocation to the multi-dimensional array ...
Avatar of nietod
nietod

>> What all other types of methods in
>> dynamic allocation can I use besides the
>> new operator
You can use malloc().  But why would you want to?

Is there any reason why you don't want to use new?

As a tottally different approach, you may wish to use a container class to store the data.  For example the STL vector<> class.

Note For 10 pts, you should be expecting a 1 or 2 word answer.  
You could use malloc or calloc . malloc is unitialized allocation and calloc is initialized. But new is the recommended mode of memory allocation in c++.
calloc is "initialized" only in the C, not C++, sense of initialized.  If the data to be allocated has a constructor, calloc does not call it.  new does.
Avatar of arindam042099

ASKER

Okay this is what I have to do:
I am writing this program for a power system which has say three transmission line and one transformer ...
frombus tobus impedance
1          2    .3
2          3    .4
3          4    .5
and the transformer data
5          3    .2

I want to allocate memory to a two dimensional array such that it becomes a 5*5 matrix ... when I do "new" I will have to read from the line data file and pick 4 and then search from the transformer data and pick 5 ... As you can see I want to avoid this searching method to size my array using the new operator ... and not only that I am talking about say 100 buses ...
I want the array to resize if I remove something out ... I DO NOT THINK I CAN DO THAT USING THE new operator ...
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks

Can you provide me with an example

Arindam
This is a 10 point question!  You got a more than 10 point answer to the original quesiton and a more than 10 point answer to a second question.  For 10 points you should ask a a question that deserves a Yes/No answer.  You should not be expecting source code.

In any case the STL is bejond the scope of ANY question.  It is well documented in many books, I recommend Bjarne Btroustrup's "The C++ programming Language"

vector< vector<double> > Matrix;

Matrix.resize(2);
Matrix[0].resize(2);
Matrix[1].resize(2);
Matrix[0][0] = 1;
Matrix [0][1] = 2

etc.;
Thanks for the help ... Sorry about the points ... When you mentioned STL earlier I had no clue what that was ...
Standard Template Library--STL.  A good C++ book will get you started.  Expect to spend a few hours learning it.  A lifetime mastering it.