[gnu.gcc] A question on local variable allocation

rfg@paris.ics.uci.edu (Ronald Guilmette) (02/24/90)

Can someone please explain to me why, in the following code, the space
allocated for the two local array variables does not overlap?

Is there some little rule in the ANSI C standard that would require such
treatment?  It would seem to me that it is not necessary, and that it
can result in a waste of space, and also possibly slower execution
(due to increased paging and an increase in cache misses).

I am posting this question to gnu.gcc rather than the gnu.gcc.bug group
because this is *not* a bug.  The code certainly works "correctly" given
the allocation strategy that GCC is now exibiting.  My question is "Would
the code still work correctly even if GCC practiced overlaping allocation
for local variables in disjoint local scopes?"

If so, why has this not been implemented.  Superfically, it would seem to
be a simple thing to do.  (Of course I acknowledge that things always seem
that way as long as you are not the guy who has to roll up his sleeves and
get in and do it.)

(Note: In the following example, the function calls are only in there to
make sure that the arrays to not get optimized away entirely.)

	extern void g();

	void function ()
	{
	  {
	     int inside1[1024];

	     g (inside1);
	  }

	  {
	     int inside2[1024];

	     g (inside2);
	  }
	}


// Ron Guilmette (rfg@ics.uci.edu)
// C++ Entomologist
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

escher@Apple.COM (Michael Crawford) (02/27/90)

In article <25E58087.11649@paris.ics.uci.edu> rfg@paris.ics.uci.edu (Ronald Guilmette) writes:
>Can someone please explain to me why, in the following code, the space
>allocated for the two local array variables does not overlap?
>
...
>	void function ()
>	{
>	  {
>	     int inside1[1024];
>
>	     g (inside1);
>	  }
>
>	  {
>	     int inside2[1024];
>
>	     g (inside2);
>	  }
>	}
>

I believe the reason is that C allows one to goto the inside of a block from
the outside, as long as the goto is within the same function.  This would
allow one to jump back and forth between the two blocks given.

I admit, though, that the optimizer ought to note the absence of any goto's or
labels within the function block, and do the allocation as you suggest,
with it overlapping.

By the way, the allocation of the arrays on the stack is done at the time the
function is entered (in general -- I do not know much about gcc).  I could
image there could be some trouble with alloca() working properly if called
inside, or outside of the blocks.

-- 
Michael D. Crawford
Oddball Enterprises
694 Nobel Drive
Santa Cruz, CA 95060
oddball!mike@ucscc.ucsc.edu

Consulting for Apple Computer Inc.
escher@apple.com

The opinions expressed here are solely my own.

casey@gauss.llnl.gov (Casey Leedom) (02/27/90)

| From: escher@Apple.COM (Michael Crawford)
| 
| >From: rfg@paris.ics.uci.edu (Ronald Guilmette)
| >
| >Can someone please explain to me why [GCC doesn't map local variables in
| >non-nested blocks into the same stack space?]
| 
| I believe the reason is that C allows one to goto the inside of a block
| from the outside, as long as the goto is within the same function.  This
| would allow one to jump back and forth between the two blocks given.

  Who cares?  Jumping out of and into blocks is the same as exiting and
entering the block contexts.  Unless the ANSI C standard says that local
block variable values should be preserved over the execution lifetime of
a function call you shouldn't expect them to contain any particular value
on entry into the block (via whatever means.)  Barring such language in
the standard, the compiler should simply allocate the hierarchal union of
all local variable storage used in the function on entry to the function.

  Would someone look this up in the ANSI C standard and see what it says
ablout the values of local block variables over the execution lifetime of
a function?  Who knows, they may have put specific language in saying
that goto's do preserve value!  Yech!!!

Casey

rfg@ics.uci.edu (Ronald Guilmette) (02/28/90)

In article <6915@internal.Apple.COM> escher@Apple.COM (Michael Crawford) writes:
>In article <25E58087.11649@paris.ics.uci.edu> rfg@paris.ics.uci.edu (Ronald Guilmette) writes:
>>Can someone please explain to me why, in the following code, the space
>>allocated for the two local array variables does not overlap?
>>
>...
>>	void function ()
>>	{
>>	  {
>>	     int inside1[1024];
>>
>>	     g (inside1);
>>	  }
>>
>>	  {
>>	     int inside2[1024];
>>
>>	     g (inside2);
>>	  }
>>	}
>>
>
>I believe the reason is that C allows one to goto the inside of a block from
>the outside, as long as the goto is within the same function.  This would
>allow one to jump back and forth between the two blocks given.

I cannot see what possible difference such jumps would make to anything.
Do you know of some secret implementation problem?  If so, please state it.

>I admit, though, that the optimizer ought to note the absence of any goto's or
>labels within the function block, and do the allocation as you suggest,
>with it overlapping.

That should happen always.  Without regard to *anything* the optimizer knows
or does (or doesn't know or doesn't do).

>By the way, the allocation of the arrays on the stack is done at the time the
>function is entered

I knew that.  I'm not totally ignorant, just mostly so.

> (in general -- I do not know much about gcc).  I could
>image there could be some trouble with alloca() working properly if called
>inside, or outside of the blocks.

Please state the problem so that us non-clarvoiants can think about it.

>Michael D. Crawford
>Oddball Enterprises

Do you manufacture oddballs or just repair them? :-)


// Ron Guilmette (rfg@ics.uci.edu)
// C++ Entomologist
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.

escher@Apple.COM (Michael Crawford) (03/01/90)

followup to gnu.misc.discuss, as requested by rms.

Many do not realize this is a mailing list, as well as a newsgroup.  Best
not to jam people's mailboxes full of philosophical debate.
-- 
Michael D. Crawford
Oddball Enterprises
694 Nobel Drive
Santa Cruz, CA 95060
oddball!mike@ucscc.ucsc.edu

Consulting for Apple Computer Inc.
escher@apple.com

The opinions expressed here are solely my own.

pa1412@sdcc13.ucsd.edu (pa1412) (03/02/90)

In article <25EAFE2C.2545@paris.ics.uci.edu> rfg@ics.uci.edu (Ronald Guilmette) writes:
>In article <6915@internal.Apple.COM> escher@Apple.COM (Michael Crawford) writes:
>>In article <25E58087.11649@paris.ics.uci.edu> rfg@paris.ics.uci.edu (Ronald Guilmette) writes:
...Can someone please explain to me why, in the following code, the space
...allocated for the two local array variables does not overlap?
.....
...	void function ()
...	{
...	  {
...	     int inside1[1024];
...
...	     g (inside1);
...	  }
...
...	  {
...	     int inside2[1024];
...
...	     g (inside2);
...	  }
...	}

From K & R the scope rule for the variables is to the matching brace.
However how the storage is allocated is clearly implementation
dependent.

What is the ANSI C scope rule defined?
--
John Clark
jclark@ucsd.edu
pa1412@iugrad2.ucsd.edu