Re: Array scope

From: Eric Sosman (eric.sosman_at_sun.com)
Date: 10/26/04


Date: Tue, 26 Oct 2004 14:00:50 -0400

Stuart Gerchick wrote:
> georgemechanic@hotmail.com (George_K) wrote in message news:<52a46bba.0410260153.149056f9@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.

-- 
Eric.Sosman@sun.com


Relevant Pages

  • Re: When is "volatile" used instead of "lock" ?
    ... If I pass it a reference to stack memory, ... *hugely* unsafe to let another thread start arbitrarily writing to ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: doubts in multi threaded application (win32 console + mfc)
    ... Thank you all (Scott McPhillips,Carl Daniel,Alex and aao) for your prompt ... What is maximum size for stack memory in multi threaded ... It highly depends on the usage and lifetime of an object, ...
    (microsoft.public.vc.language)
  • Re: Implicit int
    ... the problem is not unique to VLAs. ... Traditional C has always made auto allocation a risky business. ... There is no standard mechanism for flagging "insufficient stack ...
    (comp.std.c)
  • Re: Writing a function to figure out if stack grows up or down
    ... out if stack grows up or down. ... int a=20; ... Finally there's the issue that the order of `auto' variables ... `auto' variables reside on a stack, ...
    (comp.lang.c)
  • Re: How to avoid stack overflow in C????
    ... and total approx 450 files, each file is of approx/avg 1000 line, ... How to detect stack overflow in old big C code??? ... How to make decision of the memory to use dynamically or automatic. ... auto variable has one benefit that you need not to free the ...
    (comp.lang.c)