Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3275120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:06:36+00:00 2026-05-17T19:06:36+00:00

In C++ you can easily allocate one dimensional array like this: T *array=new T[N];

  • 0

In C++ you can easily allocate one dimensional array like this:

T *array=new T[N];

And you can delete it with one statement too:

delete[] array;

The compiler will know the magic how to deallocate the correct number of bytes.

But why can’t you alloc 2-dimensional arrays like this?

T *array=new T[N,M];

Or even like this?

T *array=new T[N,M,L];

If you want a multidimensional you have to do it like this:

T **array=new T*[N];
for(int i=0;i<N;i++) array[i]=new T[M];

If you want a fast program that uses matrices (matrix operations, eigenvalue algorithms, etc…) you might want to utilize the cache too for top performance and this requires the data to be in the same place. Using vector<vector<T> > is the same situation. In C you can use variable length arrays on the stack, but you can’t allocate them on the heap (and stack space is quite limited), you can do variable length arrays in C++ too, but they won’t be present in C++0x.

The only workaround is quite hackish and error-phrone:

T *array=new T[N*M];
for(int i=0;i<N;i++)
   for(int j=0;j<M;j++)
   {
       T[i*N+j]=...;
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:06:36+00:00Added an answer on May 17, 2026 at 7:06 pm

    Your workaround of doing T *array=new T[N*M]; is the closest you can get to a true multi-dimensional array. Notice that to locate the elements in this array, you need the value of M (I believe your example is wrong, it should be T[i*M+j]) which is known only at run-time.

    When you allocate a 2D array at compile-time, say array[5][10], the value 10 is a constant, so the compiler simply generates code to compute i*10+j. But if you did new T[N,M], the expression i*M+j depends on the value of M at the time the array was allocated. The compiler would need some way to store the value of M along with the actual array itself, and things are only going to get messy from here. I guess this is why they decided not to include such a feature in the language.

    As for your workaround, you can always make it less “hackish” by writing a wrapper class that overloads operator (), so that you could do something like array(i, j) = ....

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you have a statically allocated array, the Visual Studio debugger can easily display
In javascript you can easily create objects and Arrays like so: var aObject =
I can easily define a datatype for a node of a directed graph. data
When using PyWin I can easily load a python file into a fresh interactive
Apparently you can easily obtain a client IP address in WCF 3.5 but not
I'm looking for ActiveX components that can easily: get and send emails via SMTP
With ASP.NET 3.5 I can easily bind to an XML file by using an
On my Ubuntu linux box I can easily mount USB connected drives or CDROM
How do you organize your stored procedures so you can easily find them and
I am looking to extend jQuery so I can easily retrieve the tagName of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.