dynamic memory - need for clarification

I'm confused. Tutorial says the difference between 'normal' array and 'assigning dynamic memory to a pointer' is that normal array has to have declared size, whereas dynamic array can use a variable to declare an array (or assign memory to a pointer). i've checked this and the following code returns no errors while compiling and works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
   cout << "Enter a number: " ;
   int a=0;
   cin >> a;
   int tab[a];
   for (; a>0; a--)
      {
         tab[a]=a*a;
         cout << tab[a] << "  ";
      }
   cout << endl;
   cin.get();

   return 0;
}


there's the url:
http://cplusplus.com/doc/tutorial/dynamic.html

is there an error or did i miss something?
Last edited on
I copied the code and tried to compile on MS VS 2005 and it threw and error. It will not allow an array to be declared with a variable. I think you will find that the reason it works for you is because your using a different compiler and that particular compiler allows you to declare an array with a variable.
Last edited on
I'm using linux's c++ compiler (gcc, however the command is 'c++')...
My O/S is fedora 8.
'c++ --version' = "c++ (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)".
it throws 0 warnings or errors
so, is it correct or incorrect to use arrays this way?is it a standard?

i mean, maybe the reason are new headers/libraries and mainly 'updated' :P version of c++ (4.1.2)
-----------------------------------------------------
edit: ok there's an option '-pedantic' that solves the problem :P
Last edited on
Topic archived. No new replies allowed.