Thread View: net.lang.c++
2 messages
2 total messages
Started by Bjarne Stroustrup
Fri, 24 Oct 1986 14:57
C++ and sizeof
Author: Bjarne Stroustrup
Date: Fri, 24 Oct 1986 14:57
Date: Fri, 24 Oct 1986 14:57
50 lines
2106 bytes
2106 bytes
> Subject: C++ and sizeof
> David Eppstein @ Columbia University CS Department
>
> Is there a good reason why CC expands sizeof itself? E.g. in
>
> test.c: test..c:
> struct x { struct x { /* sizeof = 8 */
> int a; int _x_a ;
> float b; float _x_b ;
> }; } ;
>
> int foo(struct x y) int foo (_auto_y )struct x _auto_y ;
> { {
> return y.a + sizeof(y); return (_auto_y . _x_a + 8);
> } }
>
> why does the sizeof(y) become 8 and not sizeof(struct x)?
> Is there some other use CC makes of the size that makes up for the
> resulting lack of portability?
The intermediate C generated by cfront is intented to be about as portable
as assembler. In general cfront cannot do better than that since in some cases
evaluation of sizeofs are necessary for typechecking
extern v[4];
extern v[sizeof(int)]; // legal iff sizeof(int)==4
In other cases necessary to generate C from C++ constructs:
const isz = sizeof(int);
const lsz = sizeof(long);
...
switch (i) {
case 1: ...
case isz: ...
case lsz: ...
}
That said, it is also clear that cfront might generate sizeofs in many
cases thus increasing the range of constructs for which the intermediate C
is portable.
Please remember that sizeof is not the only or even the major source of
non-portability of the intermediate C. The nastiest problem is that cfront
must expand macros to generate the intermediate C . Macros defined
in header files are traditional repositories of machine dependencies.
Noteworthy examples can be found in stdio.h, ctype.h, signal.h and stdargs.h
(varargs.h).
C++ and sizeof
Author: David Eppstein
Date: Fri, 24 Oct 1986 21:36
Date: Fri, 24 Oct 1986 21:36
16 lines
1496 bytes
1496 bytes
Something I guess I didn't make clear in my original posting was that I was more interested in the effect of expanding sizeof on the portability of the C++ translator itself. I.e. it has to know enough about the machine to calculate sizeof in the first place. A compiler I worked on forces individual chars and the starts of char arrays to be word-aligned; this may not be a totally bizarre requirement but it might not have been anticipated, and surely there is worse out there. I didn't find the examples for why sizeof must be expanded particularly convincing; while CC has to calculate the sizes in those cases if is to accept and translate them, I am not sure that it should accept them in the first place. (To recap one was an array bound declared in one place as an explicit constant and in another as a size; the other was using sizes as case labels.) -- David Eppstein, eppstein@cs.columbia.edu, seismo!columbia!cs!eppstein
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads