Okay, dig this. A friend of mine asked me to show him an example of how to use dynamic array initialization in C (using user input values or variables provided by functions to init arrays, instead of hard-coding the values). So I whipped this up right quick:
#include <stdio.h>
#define ARRAY_SIZE 5
int main(void)
{
int array1size = 4;
int array2size = 5;
int lastnum = 1;
int c = 0;
int d = 0;
char choice = 0x00;
printf("Enter I to input the values manually, or D to use the pre-defined values: \n");
scanf("%c", &choice);
if ( choice == 'I') {
printf("Enter array 1 and 2 sizes (separated by a space):\n ");
scanf("%d", &array1size);
scanf("%d", &array2size);
}
else if ( choice == 'D' ) {
array1size = ARRAY_SIZE*1;
array2size = ARRAY_SIZE*2;
}
int array[array1size][array2size];
// oddly enough, if you were to input negative
// values and change these for loops to count backwards
// from 0 instead of forward, it would still work just
// fine.
for ( c = 0; c < array1size; c++ )
{
for ( d = 0 ; d < array2size ; d++ )
{
array[c][d] = lastnum*lastnum;
lastnum++;
printf("\narray[%d][%d] now contains: %d\n",
c, d, array[c][d]);
}
}
return 0;
}
Like the comment says, I found out something interesting - if you pass negative values, and change the less-thans to greater-thans, and have the loop count backwards, you'll still get a perfectly functioning array. ?? Is this a bug in GCC 3.3.4, or was C meant to do this? Or was this just something nobody ever considered, and therefore didn't see any reason to stop it?
ANOTHER interesting tidbit: if you were to pass an array initialiser an empty variable (say an int you had declared but not yet initialised), the array would still be created - and, in fact, you could assign any arbitrary value TO that array. Say, for example, we built array D with undefined index I (int d
). We could then assign anything to d[x], as long as X was defined, on to infinity (theoretically, bound only by the size of accessible memory). But the MINUTE we try to pass an undefined variable as x (like we did with i), we get a segmentation fault. Using an uninitialised int, I was able to build an array and fill it with 84095 values of int size. To wit:
#include <stdio.h>
int main(void)
{
long i;
int d;
// to prove we can pass an arbitrary constant and
// not crash things (again, assuming &d + constant*sizeof(array type)
// is within addressable memory, which it most likely will not
// always be)
d[32767] = 5;
printf("%d\n", d[32767]);
// if you take this line out and leave i uninitialized,
// the program will throw a segmentation fault and happily
// crash you back to the console.
i = 0;
while ( 1 )
{
d = i;
printf("%d\n", d);
i++;
}
return 0;
}
... Now, I made it clear to him that the first source example was the proper way to do it. But I'm puzzled as to why the second to ways work at all.
Has anyone used the functionality of either one of these instances, or have any idea why it is there? Should I report either one of these things to the good folks at GNU as a bug?
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================