Thread: variable sized arrays: compiler specific?

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    variable sized arrays: compiler specific?

    I was getting really angry at MSVC++ yesterday because I couldnt get this to compile:

    ...
    int a=0;
    int *pa;

    cin>>a;
    pa=&a;

    char aa[*pa];


    ...but i remmebered that I had done it before, so I decided to fire up DevC++ and give it a try. Sure enough, in DevC++ it works no problem. So im wondering, is having a variable sized array compiler specific (mingwn and MSVC++ in this case)? Was kinda angry since I got MSVC++ standard (after using the intro edition for over a year..) to find out that this didnt work...as I needed something simmilar to it in my current project. So right now im working with DevC++.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    no. to do dynamic arrays you use the 'new' operator
    Code:
    int array_size = 0;
    int *array = NULL;
    
    /* get the size from the user */
    cout << "Enter array size: ";
    cin >> array_size;
    
    /* now create the array */
    array = new int[array_size];
    
    /* later free the memory used */
    delete[] array;
    
    /*---- 
    | then set the pointer to NULL again 
    | so you can't use it by accident 
    ----*/
    array = NULL;
    /*** Modified for name-matching error ***/
    Last edited by taylorguitarman; 01-23-2002 at 02:29 PM.

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    The (nearly)new C standard C99 allows you to create variable length arrays on the stack as in your example. As some compiler writers don't write seperate C and C++ compilers sometimes elements from one are in another. This is why it may work in Dev C++, as the compiler is more up to date than MSVC (even though it's not legal C++).

    Also, some expect that the C99 standard will be incorporated into the new C++ standard (which is in the very early stages of being drawn up), so that it retains the 'most C programs are compatible C++ programs tag'. So sometime in the not to distant future it may be legal C++.

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    ok, thanks for the info everyone

  5. #5
    Unregistered
    Guest
    Alright, but now can I do that within classes. I tried it under the main function and it works fine. But I want to enter the variable at the command prompt and I can't get that to work. I put the class within the main function.
    This is the code:
    class NODE {
    public:
    double *array=new double[number];
    };

    NODE node;
    node.array[0]=1;

    But MS VC++ says that it is "illegal pure syntax, must be '=0'".

  6. #6
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    on first glance it should be:

    class NODE {
    public:
    double *array=new array[number];
    };

    NODE node;
    node.array[0]=1;


    because double is the datatype for the array you made, not the actual name

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Unregistered
    Alright, but now can I do that within classes. I tried it under the main function and it works fine. But I want to enter the variable at the command prompt and I can't get that to work. I put the class within the main function.
    This is the code:
    class NODE {
    public:
    double *array=new double[number];
    };

    NODE node;
    node.array[0]=1;

    But MS VC++ says that it is "illegal pure syntax, must be '=0'".
    Do it like this:

    class NODE{
    public:
    double * array;
    };

    //somewhere in your constructor, do this:

    array = new double[number];

    // and in your destructor:

    delete[] array;


    And FYI, I would never use the variable length arrays (the new, stack-based kind), for a few reasons:

    1) Many times, the stack has more limitations on memory size than the heap. The stack is supposed to be used for "smaller" operands, while large allocated blocks of memory should be on the heap.

    2) If the system cannot allocate the memory, how would you know? You can check for failure to allocate memory on the heap by checking for NULL pointers, but when you have the stack model?? You'd probably segfault, wheras in the heap scenario, you can check for the error and recover.
    Last edited by The V.; 01-24-2002 at 11:04 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  3. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM
  4. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM
  5. compiler specific
    By iain in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 08-12-2001, 08:13 PM