[M3devel] zero sized structs?

mika at async.caltech.edu mika at async.caltech.edu
Thu Oct 4 22:37:33 CEST 2012


Well as a user I'm quite happy to see the two objects have different
addresses, if it makes your life easier as a compiler writer.  However
it's certainly not documented---so I'd better not depend on it!
You're not supposed to be taking the ADR of stuff on the stack.
It's UNSAFE, caveat emptor, etc...  I'm just saying that if you are
doing that you're not writing programs that Modula-3 was intended for.
If you want to pass something by reference, use VAR!

I find that when you have trouble with zero-sized anything, you've usually
used the wrong abstraction....  In any case the fact that the language
allows certain things to be zero sized is very helpful when you are writing
programs that have to generate Modula-3 code.  You don't have to keep
track and insert dummies in various places.  But sure I can't think of any
reason the compiler couldn't implement zero-sized things as being one-sized
or four-sized, or whatever it/you want/s.  It looks like they take 16 bytes
when you NEW them... (of course the report also says that "The reference
returned by NEW is distinct from all existing references")

     Mika


Jay writes:
>Same address & same size implies same object & same type. But the types can v=
>ary. Please check also variables in sub-blocks. I think NT/x86 backend doesn=
>'t always put them at same place. Then again, that is probably ok too -- in g=
>eneral not all zero sized objects can be located -- they could be locals in d=
>ifferent functions or globals in different modules.
>
>
>You say use a different language, but 1) they are exceedingly rare so ok to w=
>aste space 2) as small & simple Modula-3 is, it is still really isn't small o=
>r simple, there are surprising number & level of detail to understand and de=
>al with. Adding a notion of a zero sized thing isn't necessarily so obviousl=
>y simple and free of complexity down the line.=20
>
>
>
> - Jay (briefly/pocket-sized-computer-aka-phone)
>
>On Oct 4, 2012, at 11:23 AM, <mika at async.caltech.edu> wrote:
>
>> It seems that CM3 puts them at the "same address"....
>>=20
>> UNSAFE MODULE Main;
>> IMPORT IO, Fmt;
>>=20
>> TYPE
>>   T =3D  ARRAY [1..-1] OF INTEGER;
>>=20
>> VAR t :=3D NEW(REF T); u :=3D NEW(REF T);
>>    v : T;
>>    w : T;
>> BEGIN
>>  IO.Put(Fmt.Int(LOOPHOLE(t,INTEGER), base :=3D 16) & "\n");
>>  IO.Put(Fmt.Int(LOOPHOLE(u,INTEGER), base :=3D 16) & "\n");
>>  IO.Put(Fmt.Int(LOOPHOLE(ADR(v),INTEGER), base :=3D 16) & "\n");
>>  IO.Put(Fmt.Int(LOOPHOLE(ADR(w),INTEGER), base :=3D 16) & "\n");
>> END Main.
>>=20
>> (114)async:~/ttt/src>../AMD64_LINUX/prog
>> 2269030
>> 2269040
>> 602218
>> 602218
>>=20
>> I don't see a problem with it.  Whoever thinks he needs to check whether
>> ADR(v) equals ADR(w) should be using a different programming language...
>>=20
>>     Mika
>>=20



More information about the M3devel mailing list