Thread: Initializing a multi-dimensional array

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Initializing a multi-dimensional array

    I need to initialize all the entries in a huge multi-dimensional array (10 * 25 * 200 * 1000) to a value. Is there any efficient way to do it rather than a 4 levels loop? Speed is my main concern. Thanks a lot for your suggestions.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    int huge_array[10][25][200][1000] = { 0 };
    Why would you need such an array ? I'm pretty sure you can find a work-around that would do the same job while being neater.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    No 'efficient' way to set it to a specific value. But if setting the array to 0, Desolation's technique works.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Initializing a huge array

    Thanks guys. I need to set all the entries to a value not equal to zero, so I guess I am stuck.

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    If your array type is int, then memset over the entire thing should be safe:
    Code:
    memset(array, 123, sizeof(array));
    If it's not an int, then individual assignment will be required, as you described.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If you used the initializer, it would be worse. The compiler would inline all 10*25*200*1000 = 50,000,000 (!!!) calls into your code. A for loop is probably all you have for now.

    But Desolation is right; a better solution is probably just waiting to be discovered.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,052
    Code:
    (10 * 25 * 200 * 1000) * 4 = 200000000
    I hope you have lots of memory.

    You should re-think your problem.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Have you thought of declaring it as a glolbal variable ?
    My knowlege is a bit rusty.. if you declare it as a global variable it should automatically get initialized to 0.

    Please correct me if i'm wrong. !!

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Machoscorpion
    Have you thought of declaring it as a glolbal variable ?
    My knowlege is a bit rusty.. if you declare it as a global variable it should automatically get initialized to 0.

    Please correct me if i'm wrong. !!
    It doesn't matter if you're right or not:
    Quote Originally Posted by alois_rone
    Thanks guys. I need to set all the entries to a value not equal to zero, so I guess I am stuck.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 07:02 AM
  2. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 05:03 PM
  3. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM