Thread: variable size of namespace?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    variable size of namespace?

    I am trying to make a namespace with an array that can change in size:

    namespace namespace2
    {
    int array[256][100][namespace1::variable];
    }

    where the value of "variable" is set earlier in the program. The problem is that my compiler (dev c++) gives me the following errors:

    storage size of namespace2::array isn't constant.
    size of variable namespace2::array is too large.

    It isnt actually too large, because It works with larger numbers than I am using when I just type them in instead of using a variable.
    How can I make the size of the namespace be set in the program?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's not a question of how big the number is, it's whether the compiler knows the value or not.

    Arrays are allocated at compile time, so all the dimensions need to be compile time constants whose value is in scope.

    A variable, or an extern const isn't going to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If the value is not known at compile-time, or the array is too big, the solution is to use dynamic memory. In C++ you would preferably use vector but you can also use new[]/delete[].

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    I got it to work with dynamic memory, thanks alot.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    Ok now I have another question that isnt really big enough to start a new thread IMO.

    How would I retrieve the total amount of system memory installed on the computer that is running the program?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    OS-specific, not that that information is worth all that much.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    windows 2000

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Even if you had that, there is always swap, other programs running along side... and many other things to affect it... that information is not exactly useless, but far from useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Array Size From Command Line Argument?
    By Cell in forum C Programming
    Replies: 6
    Last Post: 03-30-2009, 09:08 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Declaring a variable size char
    By Niara in forum C Programming
    Replies: 11
    Last Post: 10-21-2006, 01:48 AM
  4. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  5. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM