Solved

dynamically allocating multi-dimensional array using new

Posted on 1998-12-13
3
207 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now