C
Chad
On many machines that's true; though the standard imposes no such
requirement. There are machines where CHAR_BITS==16, and short is a
one-byte type. There are other machines where char is 8 bits, long
long is 64 bits, and all of the types between char and long long are
32 bits.
On many machines that's true. On other machines they can be one word
long, or a fraction of a word. The standard imposes no requirements on
that kind of thing.
Then you should revise your definition of a pointer to allow for that
possibility.
Yes, which need not be the address of a variable.
There are two different common values that code could print out, and
real machines where it will print 154 because unsigned char and short
int are both 16 bits long, and real machines where it will print
garbage, or possibly simply abort, because all integer types between
(but not including) 'char' and 'long long' are 4 bytes long.
None of which proves that the value of sint points at a variable. Your
point is?
Here is what I got
First on FreeBSD
m-net% more test.c
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
short int *sint=malloc(2);
unsigned char *a=(unsigned char *)sint;
*a=154;
*(a+1)=2;
printf("%d",*sint);
return 0;
}
m-net% gcc -v
Using built-in specs.
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.4.6 [FreeBSD] 20060305
m-net% gcc -Wall test.c -o test
m-net% ./test
666%
m-net%
Now on linux
[cdalten@localhost oakland]$ more test.c
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
short int *sint=malloc(2);
unsigned char *a=(unsigned char *)sint;
*a=154;
*(a+1)=2;
printf("%d",*sint);
return 0;
}
[cdalten@localhost oakland]$ gcc -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=release --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --
disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
[cdalten@localhost oakland]$ gcc -Wall test.c -o test
[cdalten@localhost oakland]$ ./test
666[cdalten@localhost oakland]$