Multidimensional Arrays

V

Victor Bazarov

Maximus said:
I'd like to know if it's possible to allocate a multidimensional array
during runtime using the new operator.[...]

Take a look at the FAQ. Section 16, question 16.15.
 
E

E. Robert Tisdale

Maximus said:
I'd like to know if it's possible
to allocate a multidimensional array at run-time
using the new operator.

No. But my GNU C++ compiler
g++ --version
g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

will create variable-size arrays:
cat main.cc
#include <iostream>

int main(int argc, char* argv[]) {
const int m = atoi(argv[1]);
const int n = atoi(argv[2]);
int test[m][n];
std::cout << "Hello World!" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:6: warning: ISO C++ forbids variable-size array `test'
main.cc:6: warning: ISO C++ forbids variable-size array `test'
main.cc:6: warning: unused variable `int test[m][n]'
./main 3 4
Hello World!

Variable-size arrays were recently
introduced into the ANSI/ISO C 99 standard.
Are they part of the ANSI/ISO C++ 2003 standard?
 
J

Jack Klein

Maximus said:
I'd like to know if it's possible
to allocate a multidimensional array at run-time
using the new operator.

No. But my GNU C++ compiler
g++ --version
g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

will create variable-size arrays:

> cat main.cc
#include <iostream>

int main(int argc, char* argv[]) {
const int m = atoi(argv[1]);
const int n = atoi(argv[2]);
int test[m][n];
std::cout << "Hello World!" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
main.cc: In function `int main(int, char**)':
main.cc:6: warning: ISO C++ forbids variable-size array `test'
main.cc:6: warning: ISO C++ forbids variable-size array `test'
main.cc:6: warning: unused variable `int test[m][n]'
./main 3 4
Hello World!

Variable-size arrays were recently
introduced into the ANSI/ISO C 99 standard.
Are they part of the ANSI/ISO C++ 2003 standard?

No.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
M

Maximus

Hi,
I'd like to know if it's possible to allocate a multidimensional array
during runtime using the new operator. I tried but VC tells me this:

C:\Program Files\Microsoft Visual Studio\MyProjects\asdf\asdf.cpp(9) : error
C2440: '=' : cannot convert from 'int (*)[32]' to 'int *'
Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

don't have a clue what all this mean, execpt that what I'm trying to do is
wrong =)

codes:
#include "stdafx.h"

int main(int argc, char* argv[])
{
int *test;
test = new int[32][32];

printf("Hello World!\n");
return 0;
}



TIA,Max,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top