Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dynamically allocating multi-dimensional array using new

Posted on 1998-12-13
3
Medium Priority
?
215 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

609 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