Thread View: net.lang.c++
1 messages
1 total messages
Started by Bjarne Stroustrup
Tue, 18 Feb 1986 14:53
porting C++
Author: Bjarne Stroustrup
Date: Tue, 18 Feb 1986 14:53
Date: Tue, 18 Feb 1986 14:53
38 lines
2258 bytes
2258 bytes
> From: pallas@Navajo.ARPA (Joseph Pallas)
> Newsgroups: net.lang.c++
> Subject: Re: porting C++
> Organization: Stanford University
>
> Can anyone tell me why the compiler wants some of the more obscure stuff,
> and how I can figure it out at runtime in a C program?
Most of the alignment stuff is needed to allow the compiler to calculate
sizeofs. Consider this beauty:
struct s { int a: 4; };
int v[ sizeof(struct s) ];
What is the size of struct s? Well, that depends.
On some machines every struct has a minimal size so the answer is something
like sizeof(char*). On other machines the minimal space set aside for a field
is sizeof(int). On the other hand there are machines/C-compilers that use only
sizeof(char) for struct s. This is the minimum size since a char is defined
to be the smallest unit of memory that can be independently allocated. There
are also machines where sizeof(struct s)==sizeof(int), but where
struct ss { char a : 4; };
implies sizeof(struct ss)==sizeof(char). Obscure, yes. Perverse, maybe. But
it has to be right to preserve C link-compatibility. I don't know any fully
portable way of finding all the information needed (remember this must be
done in such a way that it circumvent ``inessential'' C compiler bugs and
restrictions). Every time I think I have it, someone comes along with a C
compiler with a new twist.
C, and therefore C++, says that the type of an integer constant is int
unless the value is larger than the largest integer in which case the
type is long. Consequently a C++ compiler must know what the largest
int is. Here is one way of getting that value in C on a two's complement
machine:
int largest = ((unsigned)~0)>>1;
- Bjarne Stroustrup (AT&T Bell Labs, Murray Hill)
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