Solved

dynamically allocating multi-dimensional array using new

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixing C++ & C# in Vis Studio 2013 7 141
IdTCPClient1->Disconnect(); not working 3 61
C++ to C# code conversion issue 4 99
Problem with SqlConnection 4 160
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

12 Experts available now in Live!

Get 1:1 Help Now