![]() |
Dynamic array size
I want to create a 2-dimensional array of size N by N, where N is an integer read from a file.
:
int N; |
The short answer is that you can't do it, at least not in one step, because operator new [] only allocates a 1-dimensional array. A two dimensional array is an array one one dimensional arrays (more generally, an n-dimensional array is an array of (n-1) dimensional arrays.
One way of doing this is; :
int **array = new (int *)[N]; // array is a dynamically allocated array of pointers to int |
You could use std::vector as an alternative to built in arrays. All the memory management is done for you.
:
typedef int element; |
| All times are GMT -5. The time now is 1:01 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC