Stuart Gerchick wrote:
ge************@hotmail.com (George_K) wrote in message news:<52**************************@posting.google. com>...
[function returning pointer to `auto' array]
another solution could be to make the array static. But the array as
it stands goes away as soon as you leave the function. It might still
be around, it might not. But you are looking at something that no
longer official exists
s/official//
The disappearance of an `auto' variable when its
containing block exits is not a purely academic concern
nor a mere matter of pedantry. On most C implementations
`auto' variables are allocated on a stack, and the stack
is popped when a function returns (and sometimes when an
inner block exits, too). The next function you call (or
block you enter) will overlay the same stack memory with
its own `auto' variables, wiping out whatever the first
function (block) stored there.
Even without any subsequent function call, many C
implementations feel free to modify the stack memory
beyond the deepest current block. For example, the
context switch generated by a hardware interrupt may well
push registers, program counter, and other context onto
the stack, once again scribbling all over the memory
addressed by your bogus pointer.
On comp.lang.c we like to say things like "evaluating
`INT_MAX + 1' may make demons fly out of your nose."
From the point of view of the C language Standard this
is correct -- but in the real world it is, of course,
utter nonsense. Returning a pointer to an `auto' variable,
though, is an error of a different kind: not only *can*
it cause strange trouble, it almost certainly *will* do
so, and not only on the DeathStation 9000 but on real live
machines just like those you use every day. Don't Do That.
--
Er*********@sun.com