Array question

George Tirebiter george at moocow.uucp
Fri Feb 15 10:17:20 AEST 1991


henry at zoo.toronto.edu (Henry Spencer) writes:

> In article <EbhAAgG00WBNM2PIt_ at andrew.cmu.edu> rg2c+ at andrew.cmu.edu (Robert N
> >What happens if I do the following? Basically I don't really understand
> >*why* this works:
> >
> >    int *this_ptr;
> >    this_ptr [0] = 1;
> >    this_ptr [1] = 2;
> >    . . . 
> >    this_ptr [9] = 10;
> >
> >This works fine, but I really don't know why?? It seems you're using
> >memory to store an array which was never really allocated...
> 
> Precisely correct.  What is happening is that on your machine, whatever
> value this_ptr happens to get as its initial value happens to point to
> some memory that you are allowed to write on.  You're scribbling on a
> random piece of memory, and random things could happen as a result.
> 
> Well-designed machines try to make this a catastrophic error.

For example, Turbo C and MicroSoft C compilers point uninitialized pointers
at a copyright message embedded in the executable.  Upon exit, the copyright
message is checked for integrity and if it is corrupt (i.e., you have written
something to a pointer for which memory was never malloc()'d), you get the
message "Null pointer assignment".

Stupid compiler.  At least it's not wasting something "valuable"...

  ..George L. Tirebiter, American.
  {ames,apple}!uuwest!moocow!george or {...}uunet!nstar!syscon!moocow!george



More information about the Comp.lang.c mailing list