?
Solved

dynamically allocating multi-dimensional array using new

Posted on 1998-12-13
3
Medium Priority
?
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 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