?
Solved

dynamically allocating multi-dimensional array using new

Posted on 1998-12-13
3
Medium Priority
?
216 Views
Last Modified: 2010-04-02
Hi!

  Here is the beginner's question: How do I dynamically allocate a multi-dimensional array say int test[row][col] using the C++ 'new' operator? I tried the following:

int** test;
test = new (int*)[row];
for(int i=0;i<row;i++)
  test[i] = new int[col];

but it didn't even compile...
I also tried test = new int[row][col], no lucky either...


Can anybody please help? Also, it will be good if you can show me how to free the array using 'delete'. Thanks a lot and sorry for the point :-Ppp (that's all I have)

wyldsj
0
Comment
Question by:wyldsj
  • 2
3 Comments
 

Author Comment

by:wyldsj
ID: 1180122
Edited text of question
0
 
LVL 2

Accepted Solution

by:
trillo earned 0 total points
ID: 1180123
Supose you have A and B as your dimensions:
To use the new operator use something like this:
int **W;
W = new Complex* [A];
for (int i = 1; i < A; l++)
{
    W[i] = new int [B];
}

To use the delete operator use something like this:
for (int i = 1; i < A; l++)
{
    delete []_W[i];
}
delete []_W;


Trillo
0
 

Author Comment

by:wyldsj
ID: 1180124
thanks for your help, trillo!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question