Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!maverick.ksu.ksu.edu!ux1.cso.uiuc.edu!news.iastate.edu!sharkey!nstar!syscon!moocow!george From: george@moocow.uucp (George Tirebiter) Newsgroups: comp.lang.c Subject: Re: Array question Message-ID: Date: 14 Feb 91 23:17:20 GMT References: <1991Feb10.050642.8660@zoo.toronto.edu> Organization: Brown Cow Software (a licensed Waffle developer) Lines: 32 henry@zoo.toronto.edu (Henry Spencer) writes: > In article rg2c+@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