Python Front-end to GCC

Frank Miles fpm at u.washington.edu
Tue Oct 22 12:53:07 EDT 2013


On Tue, 22 Oct 2013 16:40:32 +0000, Steven D'Aprano wrote:

> On Tue, 22 Oct 2013 15:39:42 +0000, Grant Edwards wrote:
> 
>>> No, I was thinking of an array. Arrays aren't automatically
>>> initialised in C.
>> 
>> If they are static or global, then _yes_they_are_.  They are zeroed.
> 
> Not that I don't believe you, but do you have a reference for this?
> Because I keep finding references to uninitialised C arrays filled with
> garbage if you don't initialise them.
> 
> Wait... hang on a second...
> 
> /fires up the ol' trusty gcc
> 
> 
> [steve at ando c]$ cat array_init.c
> #include<stdio.h>
> 
> int main()
> {
>   int i;
>   int arr[10];
>   for (i = 0; i < 10; i++) {
>     printf("arr[%d] = %d\n", i, arr[i]);
>     }
>     printf("\n");
>     return 0;
> }
> 
> [steve at ando c]$ gcc array_init.c
> [steve at ando c]$ ./a.out
> arr[0] = -1082002360
> arr[1] = 134513317
> arr[2] = 2527220
> arr[3] = 2519564
> arr[4] = -1082002312
> arr[5] = 134513753
> arr[6] = 1294213
> arr[7] = -1082002164
> arr[8] = -1082002312
> arr[9] = 2527220
> 
> 
> What am I missing here?

What you're missing is that arr[] is an automatic variable.  Put
a "static" in front of it, or move it outside the function (to become
global) and you'll see the difference.



More information about the Python-list mailing list