interesting C allocation phenomenon (followup)

rcj at burl.UUCP rcj at burl.UUCP
Tue Jun 21 00:26:54 AEST 1983


It was mentioned that there is a problem with overlays of character
strings which have no dimension given.  The example below was cited:
-------------------------------------------------------------------
main() {
	char s[];
	int i;

	i = 0x12345678;
	s[0] = 'a';

	printf("i = 0x%08x\n",i);
}
--------------------------------------------------------------------

I am running USG 5.0 on a Vax 11/780.  Using sdb, I found the following
allocations:

s	0x7ffff278
i	0x7ffff274

Clearly, there is not conflict here.  Then I tried this declarations
section instead:

	char s[],t[];
	int i;

This yielded:

s	0x7ffff278
t	0x7ffff278
i	0x7ffff274

Again, i was safe from harm, but t was overwritten by s (or vice versa).
My last fling was:

	char s[];
	int i;
	char t[];

This gave the horrifying result of:

s	0x7ffff278
i	0x7ffff274
t	0x7ffff274

i was overwritten by t this time!!!

I can see a hazy pattern emerging here -- does anyone want to clarify
it for me?  I would be most appreciative.  Unless you are sure that it
will be of general interest, you may wish to stick to mail to keep netnews
traffic down.

Thanks 4 ur time,
-- 

The MAD Programmer -- 919-228-3814 (Cornet 291)
alias: Curtis Jackson	...![ floyd sb1 mhuxv ]!burl!rcj



More information about the Comp.lang.c mailing list