Link to home
Start Free TrialLog in
Avatar of s_l
s_l

asked on

Best way to initialize arrays

In my code I have the following declaration of Enums (I'm writing in C++ so I don't use typedef and such, but the question is for C):

enum MyColor {
   color1 = 0,
   color2,
   color3,
   color4,
   color_invalid
};

enum MyType {
   type1 = 0,
   type2,
   type3,
   type_invalid
};

Now I want to declare an array in the size of the MyType enum of type MyColor initialize it. Inside my include file I wrote:

static MyColor array[];

Inside my C code I wrote:
MyColor array[type_invalid] = { color1, color2, color3 };

The question is:
When the enum MyType changes and gets bigger I don't get any warning/error from the compiler on the line of initializing the array. Instead I get uninitialized partitioned of the array. Is there a way/trick so that the code can be safer and protect me from this problem? Maybe the compiler can issue some warning/error?
Avatar of aceventura
aceventura

Hi !
   The compiler will not prevent you from accessing the
memory locations outside your declaration. You may have
declared array[4], but even if you try to access a[6], the
compiler will not complain, nor will it give you an error.
   If you want to trap this error, there is no direct way.
You will have to resort to round about methods using the
sizeof operator.
   See the example that I have written.

/* Size.c */
#include <stdio.h>

void main()
{
 int  array[] = { 1, 2, 3, 4, 5 };  
 /* sizeof( array ) is 20         */
 /* sizeof( array[0] ) is 4       */
 /* Count of items in array       */
 int Index;

 size_t  sizearr = sizeof(array) / sizeof(array[0]);

 printf("Array size = %d", sizearr);

 printf("\nEnter Index to get : ");
 scanf("%d",&Index);
 if (Index > sizearr)
    printf("\nYou have specified an Index out of bounds.");
 else
    printf("\nElement at location %d is %d.",
                            Index, array[Index]);
 getch();
}



Hope this helps.
Regards,
ace
You say you are writing in C++ but the question is for C. Does that mean you want a solution for C code or can the solution involve C++ objects and constructors?
I think I understand the problem now...

Are you asking how to get the compiler to warn you about array initialisation that contains less elements than the defined size of the array?
If I understand ok You want to have properly initialized array. You may write some initialize function to do that.
This is the correct way.
Another way is to implement this query somewhere in the start of your program.
if( MyColor[color_invalid-1] != color_invalid )
   ;//Error

Same thing with types.
ASKER CERTIFIED SOLUTION
Avatar of MadYugoslav
MadYugoslav
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of s_l

ASKER

zebada - this is exactly what I need. But without any tricks of C++.

aceventura - thanks for the help but it's not what I was looking for.

MadYugoslav - this is a nice idea (to check if the last element is valid or not. But how can I be sure of what the compiler puts inside if I initialized it with too few parameters? I know that in optimized mode the compilers usually don't clear arrays...
Ok, if I understand you correctly, you can not make this check at compile time. Instead, put a for-loop in the beginning and make the initialization there.
You will not have an "uninitialized" section of your array.  If you do not provide enough initializers for an array, the unspecified items will automatically be set to 0 (as per the C standard).

So, if you have this declaration:

int x[5] = {1};

it is the same as if you had specified:

int x[5] = { 1, 0, 0, 0, 0 };
Avatar of s_l

ASKER

Thanks. I will go with the query you suggested.