Thread: Initializing array of variable size

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Initializing array of variable size

    Hello all.

    I would like to initialize the elements of an array at compilation. However, the size of the array may change beyond my control (several people work on the project), and I would like to make my code the most generic possible.

    What I did previously is this :
    Code:
    int tmpArray[SIZE] = {[0 ... (SIZE-1) = 10};
    And that works fine, as long as all the values have to be the same.
    It's possible to use functions , as in :
    Code:
    int tmpArray[SIZE] = {[0 ... (SIZE-1) = pow(10,10)};
    But still, all the values of the array are the same.

    What I wish to do is to initialize the values, but not to the same value. I would like to have the values of the array be :
    10^0, 10^1, 10^2, ..., 10^(SIZE-1)

    But I have not found a way of doing this. Using 'for' in the initialization brackets doesn't work (or at least, I haven't found a way of doing so), and I haven't found if there is an internal loop variable that I can use.

    I tried the following :
    Code:
    int i=0;
    int tmparray[SIZE] = {[0 ... (SIZE-1)] = pow(10, i++)};
    But it didn't work, it assignes the value of pow(10, 0), i.e. 1, to all of the elements in the array, then increments the value of 'i' to 1.

    I would like to avoid having to initialize the array in a loop during the program, since the values in the array are constant.

    Any help in this would be much appreciated.

    Anton

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Anton
    I would like to initialize the elements of an array at compilation. However, the size of the array may change beyond my control (several people work on the project), and I would like to make my code the most generic possible.
    I think that you should just mandate that should the size of the array be changed, the initialiser should also be changed. A comment at the definition of SIZE is a first step, and then you can write an assertion after the array initialisation (or some other suitable point) to check.

    Otherwise, I suggest just assigning to the elements of the array rather than trying to use the initialiser.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by laserlight View Post
    I think that you should just mandate that should the size of the array be changed, the initialiser should also be changed. A comment at the definition of SIZE is a first step, and then you can write an assertion after the array initialisation (or some other suitable point) to check.

    Otherwise, I suggest just assigning to the elements of the array rather than trying to use the initialiser.
    I did think of leaving a comment near the size definition, but wanted to avoid having so that people don't have to go through all bits of code related to this define. I only mentionned one array, but there are several to deal with.

    If there is no solution that can be applied at the creating, I will write a function to initialize everything at the beginning, but I wanted to avoid this.

    Thanks, though.

    Anton

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why not just write a function?
    Code:
    int tmpArray(int x)
    {
        return (int)pow(10.0, (double)x);     /* there are better ways than this */
    }
    No need for an array at all.

    There is also a slight issue that int representation is finite. The standard only guarantees that an int can hold a value to 32767 (although a 32-bit int can hold up to 2147483647).
    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.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    @grumpy This would be sufficient if I was only using 'pow(...)'. But I only mentionned it to simplify the example.
    The values (limited to 10, so a 32-bit int would be sufficient) would be :
    1, pow(10, 1)+1, pow(10, 2)+1, ..., pow(10, SIZE-1)+1

    I wanted to use arrays to replace a series of already existing #defines, that were written when SIZE=2, so it wasn't much trouble to just make a copy/paste and change a '1' to a '2' in the define name. I was hoping to be more generic in the re-write.

    But it seems that the re-write with have to include a function call to initialize the array.
    Oh well ...

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Anton View Post
    But it seems that the re-write with have to include a function call to initialize the array.
    Or a simple loop after the definition of the array.
    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
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    @grumpy : Which is what I was hoping to avoid. The declaration is in a header file that's used for different projects. I was hoping to avoid having to place the initialization loop in several files, and just have one at the declaration in the header file.
    But it seems that this isn't possible. I guess it's one point in favour of OOP.
    Thanks for the help.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Anton View Post
    @grumpy : Which is what I was hoping to avoid. The declaration is in a header file that's used for different projects. I was hoping to avoid having to place the initialization loop in several files, and just have one at the declaration in the header file.
    But it seems that this isn't possible. I guess it's one point in favour of OOP.
    Thanks for the help.
    Does the header file attach to any particular libarary?
    You could add a function to that library to intialize the array.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by CommonTater View Post
    Does the header file attach to any particular libarary?
    You could add a function to that library to intialize the array.
    I wish.
    The header file is just a header file, with a load of #defines, struct declarations, and the like.
    I think I'll have to go in the direction laserlight said, and leave a note with explicit instructions of what to change if the value changes in the future.
    Thanks for the help.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It strikes me your requirement of having to initialise the array in the header file is unrealistically restrictive. Particularly since you are clearly able to change the header file: header files are generally more stable (i.e. they change less often) than corresponding source (eg .cpp) files. And if you can use a single header in multiple projects, you can use a single header and a corresponding source file in multiple projects.
    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.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Consider using a compile-time assert C_ASSERT to validate that the last element of the array is non-zero. Since unspecifies initialisers are always zero, this would catch any increase to the size of the array without adding more initialisers at compile time.
    Code:
    const int SIZE = 6;
    
    const int tmpArray[SIZE] = {1, 10, 100, 1000, 10000, 100000};
    C_ASSERT(tmpArray[SIZE-1] != 0); // You must add initialisers to the above if you change SIZE
    That might not work if C doesn't consider the array to be const-enough.

    The other option is to have the array declared of each different size but use a #define to determine which one is active:
    Code:
    #define THE_SIZE 6
    
    const int SIZE = THE_SIZE;
    #if THE_SIZE == 1
    int tmpArray[SIZE] = {1};
    #elif THE_SIZE == 2
    int tmpArray[SIZE] = {1, 10};
    #elif THE_SIZE == 3
    int tmpArray[SIZE] = {1, 10, 100};
    #elif THE_SIZE == 4
    int tmpArray[SIZE] = {1, 10, 100, 1000};
    #elif THE_SIZE == 5
    ... // Yours would of course go all the way to 10
    #endif
    Option 3, convert the project to C++ and use template meta-programming!
    Last edited by iMalc; 11-23-2011 at 02:46 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Quote Originally Posted by iMalc View Post
    Option 3, convert the project to C++ and use template meta-programming!
    I'm gonna have to ease myself back into C++, which I haven't touched in about 4 years, for personnal project(s) ... Such fun!!

    Anyhoo, thanks to all for your help. I believe this thread can be set to 'resolved'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable Array Size
    By bchudomelka in forum C Programming
    Replies: 17
    Last Post: 05-19-2010, 09:26 PM
  2. Array size set by variable
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 11-20-2009, 03:13 PM
  3. variable array size
    By shuo in forum C++ Programming
    Replies: 39
    Last Post: 01-29-2008, 06:39 PM
  4. array of a variable size
    By dougwilliams in forum C Programming
    Replies: 3
    Last Post: 11-17-2007, 11:55 PM
  5. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM