Thread: Array of Flags

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    Array of Flags

    Hey so I have an array of flags ( either one or zero) and I would like to make it a global variable so I can change it in any function. The problem is that I don't know how big the array has to be until I get data from an input file so I can't define how big the array will be until I run a different function which means I cant define its size when I declare it as a global variable. How would I do this? or how would I pass the array to another function with pointers?

    Thanks,
    if this is too vague let me know

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Too vague, show code.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You could use an int with "bit flags", if you are comfortable enough with bit operations (if not, now is the time to learn).

    A single int variable can hold 32 or 64 separate (boolean) flags this way, so (presuming you have only a dozen or so flags) you won't need an array.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    Well its alot of code and I don't really know if it will help but Ill try to clarify and give an example.

    inset
    Code:
    int flags[ ][ ];
    
    void updateFlags(int x, int y)
    {
       flags[x][y]=1;
       return;
    }
    
    int main(int argc, char **argv)
    {
        int sizeOfX, sizeOfY (pretend these already have some value)\
        int x,y;
        int i, j;
        flags[sizeOfX][sizeOfY];
        for(i=0;i<sizeOfX)
        {
           for(j=0;j<sizeOfY)
           {
               flags[i][j]=0;
           }
        } 
        updateFlags(x,y);
    
    }
    thats basically the gist or example of what I want to do. I have a multidimensional array that I want to be a global variable so I can update it whenever but I can't define its size until part way through the program when I have more information.

    Does that make more sense?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Better than using a global variable would be to pass the array to each function that needs it.

    To have an array of a run-time determined size, you need to allocate memory with malloc() or calloc(). You store this memory in a pointer, and when done with it, free() the memory. Also usefull is the realloc() function, for resizing a dynamic array like that. Read up of these functions.

    Flags would generally be char or perhalps unsigned or signed char, unless you want to save memory by cramming them into an integer type. With a dynamic size this is not a good idea.

    As to how to pass an array to a function: a 1D array decays to a pointer to the first member when passed to a function. So a char pointer can be use to pass a char array. You can also use array syntax in the parameter list, but the optional array size will be ignored.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    well its more complicated then that because its a multidimensional array (mine is actually 5) so I need to be able to specify x,y,z,t, and w(wavelength) so bitwise probably wont be big enough.

    In case you're interested, its for a marching cubes algorithm and I need to know which cubes I have processed triangles for already because I most likely will not be going in order.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    8
    ya im currently trying to figure out how to pass it from function to function but i think i just have bad syntax so im trying to figure it out

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    A multidimensional arrays are trickier. There are two ways multidimensional arrays are written. One is an array of pointers, to an array of values. This is easier to work with for dynamic arrays, for syntax reasons though it does add more overhead.

    The other way is to create a continuous block of memory and use the formula (ptr+row*sizeof(element)*max_col+col) to access individual values. This can be extended to 3 d arrays. It is how C handles it's multidimensional arrays, but those cannot have a dynamic size.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Passing a 2d array looks like one of these idential options:
    Code:
    int func1(char array[5][32]);
    int func2(char array[][32]);
    int func3(char (*array)[32]);
    But it won't work if you don't know the second dimension.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by seubjoh View Post
    well its more complicated then that because its a multidimensional array (mine is actually 5) so I need to be able to specify x,y,z,t, and w(wavelength) so bitwise probably wont be big enough.
    Hmmm. Well, just since I went and dug up my bit flag demo (bear with me):
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int checkbit (int value, int bit) {
    	if (bit==1) return (value&=1 ? 1 : 0);
    	return ((value&=(int)pow(2,bit-1)) ? 1 : 0);
    }
    
    void showset (int value) {
    	int i;
    	for (i=1;i<=32;i++) if (checkbit(value,i)) printf("bit #%d is set\n",i); 
    }
    
    int main (void) {
    	int Flags = 0;
    	Flags ^= 2;   /* set 2nd bit flag */
    	Flags ^= 16;  /* set 5th bit flag */
    	Flags ^= 32768;  /* set 16th bit flag */
    	Flags ^= 67108864;  /* set 27th bit flag */
    	showset(Flags);
    	puts("---------------");	
    	Flags ^= 2;   /* unset 2nd bit flag */
    	Flags ^= 32768;  /* unset 16th bit flag */
    	showset(Flags);
    	/* check 27th bit */
    	if (checkbit(Flags,27)) puts("27 is set"); 
    	/* check 28th bit */
    	if (checkbit(Flags,28)) puts("28 is set"); 
    	return 0;	
    }
    If there are 12 cubes or less, you could just add some math to your flag checking functions to pull x,y,z,t,w from a long int.

    If there are more than 12 cubes, you could bit flag them individually, which means no need for a multi-dimensional array, and/or you could pass around single local variables (each cube is a char of bit flags) instead of using globals. You can pass a char *string of indeterminate length, making sure each cube has at least one bit set so to differentiate it from '\0' (eg, so a strlen() will give you the number of cubes to process). So you could even make that a global:
    Code:
    char *Cubes;
    and when you add a cube, realloc() *Cubes and add a byte of data to it. Plus you even have room for an R, G, B for each cube
    Last edited by MK27; 08-25-2009 at 04:38 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's a pretty generic approach (can be used with any data type, really):

    Code:
    #include <stdio.h>
    
    /*
    	It really isn't necessary to require a T***, but it makes calling the macro work 
    	like a similar function call would in this context - a C template function, if you will.
    */
    #define ALLOCATE_MATRIX( ptr, rows, cols )\
    do\
    {\
    	size_t\
    		idx = 0;\
    	*ptr = malloc( sizeof( **ptr ) * rows );\
    	while( idx < rows )\
    	{\
    		( *ptr )[ idx++ ] = malloc( sizeof( ***ptr ) * cols );\
    	}\
    }\
    while( 0 )
    
    #define DEALLOCATE_MATRIX( ptr, rows, unused )\
    do\
    {\
    	size_t\
    		idx = 0;\
    	while( idx < rows )\
    	{\
    		free( ( *ptr )[ idx++ ] );\
    	}\
    	free( *ptr );\
    	*ptr = 0;\
    }\
    while( 0 )
    
    int**
    	flags = 0;
    
    int main( void )
    {
    	ALLOCATE_MATRIX( &flags, 1024, 128 );
    	flags[ 511 ][ 63 ] = 574;
    	DEALLOCATE_MATRIX( &flags, 1024, 128 );
    	return 0;
    }
    hth.
    Last edited by Sebastiani; 08-25-2009 at 07:23 PM. Reason: rows/columns reversed

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    When templates aren't an option, macros are your friend!
    (Half joking. Macros aren't type safe, so avoid them if possible.)
    Also, I would think that flags could be moved inside main and passed to functions as necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM