🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1193

Re: struct hack, and other out-of-array references

#1193
From: Fergus James HENDERSON
Date: Fri, 11 Sep 1992 14:07
57 lines
2600 bytes
flaps@dgp.toronto.edu (Alan J Rosenthal) writes:

>dat@thinkage.on.ca (David Adrien Tanguay) writes:
>>How is this any different from the array example?  p may point
>>outside a[1], but it is still within the a object.  Similarly, ...
>>is outside the f->s object but within the malloc object.
>
>The point is, "int a[5][5];" doesn't give you 25 ints, it gives you five arrays
>of five ints each.  "(int(*)[5])malloc(5*sizeof(int[5]))" gives you
>sizeof(int)*25 bytes which are ALSO five arrays of five ints.
>
>The first looks like this:
>	int[5][5]:
>	    int[5]:
>		int
>		int
>		int
>		int
>		int
>	    int[5]:
>		...
>	    ...
>
>And the second looks like this:

     malloc(25):
>	char
>	char
>	char
>	char
>	...
>
>Now that cast magically makes the second be close enough to the first in
>outward appearance for all intents and purposes, but it's still the second
>underneath.  Obviously in almost all compilers the memory layout will be the
>same.  However, an environment such as Saber-C should be free to keep track of
>the details and that's why I've always thought that this is a good
>interpretation ruling.
>
>I'm not sure what you get when you take a pointer to that array five of array
>five of int, cast it to a (char *), then cast it to a (int(*)[5]).

This sounds like a fairly reasonable explanation, but I still have some
questions.

Do same considerations would apply if you tried to use a char[5][5] 
array as a char[25] array? It sounds like you would say they
do. What about casting a char[25] array to a char[5][5] array?

From what you're saying, the reason that casting the return
value from malloc succeeds is only because malloc is "magic".

-- 
Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
This .signature virus is a self-referential statement that is true - but 
you will only be able to consistently believe it if you copy it to your own
.signature file!
Technical Details
Message-ID: <9225600.28053@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
References: <1992Sep07.104932.20060@x.co.uk> <1992Sep8.124655.1498@Urmel.Informatik.RWTH-Aachen.DE> <1992Sep10.014137.16209@sq.sq.com> <1992Sep10.213240.10272@thinkage.on.ca> <1992Sep11.012438.24377@jarvis.csri.toronto.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c
Path: arc1-ca.pugleaf.net!sparky!uunet!usc!rpi!batcomputer!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
Subject: Re: struct hack, and other out-of-array references
Message-ID: <9225600.28053@mulga.cs.mu.OZ.AU>
Sender: news@cs.mu.OZ.AU
Organization: Computer Science, University of Melbourne, Australia
References: <1992Sep07.104932.20060@x.co.uk> <1992Sep8.124655.1498@Urmel.Informatik.RWTH-Aachen.DE> <1992Sep10.014137.16209@sq.sq.com> <1992Sep10.213240.10272@thinkage.on.ca> <1992Sep11.012438.24377@jarvis.csri.toronto.edu>
Date: Fri, 11 Sep 1992 14:07:47 GMT
Lines: 57