Hello, is this thing on?

This is a discussion on Hello, is this thing on? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; void create(const int); int main() { const int s=26; create(s); } void create(const int ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Question Hello, is this thing on?

    Code:
    #include <iostream>
    using namespace std;
    
    void create(const int);
    
    int main()
    {
    const int s=26;
    create(s);
    }
    
    void create(const int s)
    {
    const int a=26;
    int c[s];
    }
    The above program will not compile and shows "error c2057: expected constant expression." However, variables s clearly is a constant expression. If I change the "s" to "a" in "int c[s]" the program runs fine. This makes absolutely no sense- both variables are of the same type and are both constant, but the program will compile with one and not the other. The software environment is MVS 2005, but I've tried others with the same results. Can anyone tell me why? Thanks, Jeff Kurtz

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Because the compiler will know how big the stack frame will need to be when you use a, but s could be anything. If you need to dynamically allocate some memory use the heap.

  3. #3
    Always learning
    Join Date
    Apr 2009
    Posts
    25
    I used g++ for the above code and it compiled.

    Why are you declaring an array with a variable size? Not all compilers can support it.

    EDIT: too late :P

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Variable sized arrays is a C99 feature if I'm not mistaken, and VC++ doesn't support C99.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by cpjust View Post
    Variable sized arrays is a C99 feature if I'm not mistaken, and VC++ doesn't support C99.
    I didn't know that "variable" sized arrays on the stack could exist? How does this work under the hood?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    I didn't know that "variable" sized arrays on the stack could exist? How does this work under the hood?
    The compiler produces the relevant calculations to allocate stack-space. Just like for fixed size arrays, but with the difference that the inputs is a variable.

    As stated, this is not supported by all compilers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pause/idle thing
    By freedik in forum Windows Programming
    Replies: 13
    Last Post: 08-22-2003, 09:46 AM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 12:43 PM
  3. most challenging thing to program
    By volk in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 03-28-2003, 02:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 08:23 PM
  5. PingPong But how to make 2 thing at the same time..
    By Gugge in forum C Programming
    Replies: 5
    Last Post: 04-02-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21