Thread: Let The User Specify Array Size

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Question Let The User Specify Array Size

    I am not sure why I am receiving the error message:
    error C2466: cannot allocate an array of constant size 0
    When I run the code:
    Code:
     int s;
     cout<<"Enter the size:\n";
     cin>>s;
     int array[s];
    C++ masters, please help me.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The current C++ standard requires array sizes to be compile time constants. I recommend you think about using std::vector instead of the array, but if you insist on arrays then you'll need to dynamically allocate the memory using new/delete.


    Jim

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    Post

    Quote Originally Posted by jimblumberg View Post
    The current C++ standard requires array sizes to be compile time constants. I recommend you think about using std::vector instead of the array, but if you insist on arrays then you'll need to dynamically allocate the memory using new/delete.


    Jim
    Thanks, I got it to work.

  4. #4
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by jimblumberg View Post
    The current C++ standard requires array sizes to be compile time constants. I recommend you think about using std::vector instead of the array, but if you insist on arrays then you'll need to dynamically allocate the memory using new/delete.


    Jim
    I am wondering if something change lately because I expect this king of behavior but gcc accepts this code.

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Well, AFAIK, this should be in C99 standard/C++98, so this is supposed to work.

    [edit]

    I think its a gcc extension when it comes to C++ code(Its not there in the C++ standard).

    [edit]
    Last edited by Aslaville; 10-13-2014 at 02:39 AM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Aslaville View Post
    Well, AFAIK, this should be in C99 standard/C++98, so this is supposed to work.
    VLAs (Variable Length Arrays) are specified in C99, not in any C++ standard.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by grumpy View Post
    VLAs (Variable Length Arrays) are specified in C99, not in any C++ standard.
    Yes, that is the reason I edited.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The gcc compiler has "hacks" that allow VLA, and some other "features", but the current C++ standard C++11 specifically disallows this "feature". To force gcc to avoid the "hack" you must use a compiler switch to turn off the "hack" (and most other compiler specific "features") and abide by the standard (-Wpedantic). See this [URL="https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options"]link[/URL and note by default gcc doesn't issue warnings for anything other than "required" warnings, but by using various options can issue many more types of warnings.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Size of Array, user inputted..
    By otchster in forum C++ Programming
    Replies: 19
    Last Post: 06-25-2007, 12:50 PM
  2. Array Help Please (user defined size)
    By Planetx33 in forum C++ Programming
    Replies: 9
    Last Post: 04-07-2007, 04:36 PM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. array user definable size
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2004, 07:48 PM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM

Tags for this Thread