不同体系结构上的Sizeof(X) [英] Sizeof(X) on different architectures

查看:87
本文介绍了不同体系结构上的Sizeof(X)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



hy ppl,我想在这里创建一些多平台软件,我对不同机器上常见变量的大小非常好奇。


如果你运行的东西不同于桌面下的32位x86盒子,请检查这个程序输出的内容。


// - 程序启动 -


#include< stdio.h>


#define showsize(x,y)printf("%s%d \的大小) n",x,sizeof(y));


typedef struct db_s {

int offset;

int length;

char * data;

} db;


int main(){

showsize(" ; char",char);

showsize(" int",int);

showsize(" long",long);

showsize(" char *",char *);

showsize(" int *",int *);

showsize(" long *" ;,* *;

showsize(" void *",void *);

showsize(" db",db);

showsize(" db *",db *);

返回0;

}


// - - 程序结束 -

32位x86上的
报告如下:


char 1的大小

int 4的大小
长4的大小

int的大小* 4

尺寸长* 4

尺寸无效* 4

db 12尺寸

db尺寸* 4

......


那里有64位太阳用户吗? 64位x86机器用户和mac用户?


如果可以请报告:)


hy ppl, i''m trying to create some multiplatformed software here and i''m very curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your desk, please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;

int main() {
showsize("char",char);
showsize("int",int);
showsize("long",long);
showsize("char*",char*);
showsize("int*",int*);
showsize("long*",long*);
showsize("void*",void*);
showsize("db",db);
showsize("db*",db*);
return 0;
}

//-- program ends --

on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4
......

are there any 64bit sun users out there ? 64bit x86 machine owners and mac users ?

if you can please report in :)

推荐答案

some c编译器似乎与#define行有问题,

所以这里是一个无定义版本


#include< stdio.h>

typedef struct db_s {

int offset;

int length;

char * data;

} db;


void report(char * name,int size){

printf("%s%d \ n的大小),名称,大小);

}


int main(){

报告(" char",sizeof(char)) ;

报告(" int",sizeof(int));

报告(long,sizeof(long));

report(" char *",sizeof(char *));

report(" int *",sizeof(int *));

报告(long *,sizeof(long *));

报告(" void *",sizeof(void *));

报告(" ; db",sizeof(db));

报告(" db * ",sizeof(db *));

返回0;

}


Martin Roos写道:
some c compilers seem to have issues with the #define line,
so here is a defineless version

#include <stdio.h>
typedef struct db_s{
int offset;
int length;
char* data;
} db;

void report(char *name, int size) {
printf("Size of %s %d \n",name,size);
}

int main() {
report("char",sizeof(char));
report("int",sizeof(int));
report("long",sizeof(long));
report("char*",sizeof(char*));
report("int*",sizeof(int*));
report("long*",sizeof(long*));
report("void*",sizeof(void*));
report("db",sizeof(db));
report("db*",sizeof(db*));
return 0;
}

Martin Roos wrote:

hy ppl,我想在这里创建一些多平台软件,我对不同机器上常见变量的大小非常好奇。
如果你在
桌面下运行的东西不同于32位x86盒子,请检查这个程序输出的内容。

// - 程序启动 -

#include< stdio.h>
#define showsize(x,y)printf("%s%d \ n的大小",x,sizeof(y));

typedef struct db_s {
int offset;
int length;
char * data;
} db;

int main(){
showsize(" char",char);
showsize(" int",int);
showsize(" long",long);
showsize(" char *",char *);
showsize(" int *",int *);
showsize(" long *",long *);
showsiz e(void *,void *);
showsize(" db",db);
showsize(" db *",db *);
返回0 ;
//
// - 程序结束 -

在32位x86上报告如下:

char的大小1
int 4的尺寸
长4的尺寸
炭的尺寸* 4
尺寸* 4
尺寸长* 4
尺寸void * 4
db 12的大小
db * 4的大小

.....

那里有64位太阳用户吗? 64位x86机主和
mac用户?

如果可以请报告:)

hy ppl, i''m trying to create some multiplatformed software here and i''m
very curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your
desk, please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;

int main() {
showsize("char",char);
showsize("int",int);
showsize("long",long);
showsize("char*",char*);
showsize("int*",int*);
showsize("long*",long*);
showsize("void*",void*);
showsize("db",db);
showsize("db*",db*);
return 0;
}

//-- program ends --

on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4
.....

are there any 64bit sun users out there ? 64bit x86 machine owners and
mac users ?

if you can please report in :)



Martin Roos写道:
Martin Roos wrote:
#define showsize(x,y)printf("%s%d \ n的大小",x,sizeof(y));
showsize(" char",char);
showsize(" char *",char *);
showsize(" void *",void *);
字符大小1


sizeof(字符)总是一个。

字符大小* 4
无效大小* 4
#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y)); showsize("char",char); showsize("char*",char*); showsize("void*",void*); Size of char 1
sizeof(char) is always one.
Size of char* 4 Size of void* 4




sizeof(char *)等于sizeof(void *),

,只要它们在同一个实现上。


-

pete



sizeof(char*) is equal to sizeof(void*),
as long as they''re on the same implementation.

--
pete


文章< 41 ******* ***@news.estpak.ee> ;, Martin Roos< ma **** @ none.ee>

写道:
In article <41**********@news.estpak.ee>, Martin Roos <ma****@none.ee>
wrote:
hy ppl,我正在尝试在这里创建一些多平台软件,我非常好奇不同机器上常见变量的大小。

如果你运行的东西不同于32-你的桌子下面有一个x86盒子,
请查看这个程序输出的内容。

// - 程序启动 -

#include< stdio.h>

#define showsize(x,y)printf("%s%d \ n的大小",x,sizeof(y));

typedef struct db_s {
int offset;
int length;
ch ar * data;
} db;

....
在32位x86上报告如下:

char 1的大小
int 4的大小
长4的大小
char的大小* 4
int的大小* 4
长的大小* 4
无效的大小* 4
db 12的大小
db * 4的大小
hy ppl, i''m trying to create some multiplatformed software here and i''m very
curious about the sizes of common variables on different machines.

if you are running something different than a 32-bit x86 box under your desk,
please check what this program outputs.

//-- program starts --

#include <stdio.h>

#define showsize(x,y) printf("Size of %s %d \n",x,sizeof(y));

typedef struct db_s{
int offset;
int length;
char* data;
} db;
....
on an 32bit x86 it reports like this :

Size of char 1
Size of int 4
Size of long 4
Size of char* 4
Size of int* 4
Size of long* 4
Size of void* 4
Size of db 12
Size of db* 4




32位sparc是完全相同的。我知道使用LP64的64位sparc和所有最新的64位Unix

。模型 - 长指针和指针是64位,

整数是32位。我认为Windows 64位使用(疯狂的,恕我直言)LLP64
模型 - 长long和指针是64位,long和int是32位。


典型的Unix和Windows 32位案例被称为ILP32

(整数,长整数和指针都是32位)。


无论如何,在Solaris sparc上编译64位时的输出是:


char 1的大小

大小int 4

长8尺寸
字符大小* 8

int尺寸* 8

尺寸长* 8

无效大小* 8

db 16的大小

db * 8的大小


干杯,

- jonathan



32-bit sparc is identical. 64-bit sparc, and all recent 64-bit Unixes
that I know of use the "LP64" model -- longs and pointers are 64-bit,
ints are 32-bit. I think Windows 64-bit uses the (crazy, IMHO) "LLP64"
model -- long longs and pointers are 64-bits, longs and ints are 32-bit.

The typical Unix and Windows 32-bit case has been referred to as "ILP32"
(ints, longs, and pointers are 32-bits).

In any case, the output when compiled 64-bit on Solaris sparc is:

Size of char 1
Size of int 4
Size of long 8
Size of char * 8
Size of int * 8
Size of long * 8
Size of void * 8
Size of db 16
Size of db * 8

Cheers,
- jonathan


这篇关于不同体系结构上的Sizeof(X)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆