使用typedef的优点 [英] advantage of using typedefs

查看:88
本文介绍了使用typedef的优点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看linux的源代码或打开BSD。我发现

使用了很多typedef

。例如,考虑文件中的偏移量。它被宣布为



off_t offset;

和off_t是typedefed为

typedef long off_t;


我想知道typedef有什么优势?为什么我们没有

声明

抵消仅仅为

long off_t;


类似于文件大小的情况等等.....


在这种情况下,有没有人知道为什么我们使用typedef?


Thanx任何帮助提前....

解决方案

ju ********** @ yahoo.co.in écrit:

我想知道是什么我们通过typedef获得的优势?为什么我们没有
声明
偏移只是为了
long off_t;




它通常是一个可移植性问题。想象一下,你想要一个能够保持

最大可能的无符号整数的类型。


你将在C90中拥有这个:


typedef unsigned long largest_int_t;


和C99:

typedef unsigned long long large_int_t;


在一些智能宏和#ifxxx指令的帮助下,经过一次简单的重新编译后,在简单的重新编译项目之后,该类型将自动更正/ lib或其他。


-

A +


Emmanuel Delahaye




Emmanuel Delahaye写道:

ju ********** @ yahoo.co.in 中écrit:

我想知道typedef有什么优势?为什么我们没有
声明
简单地偏移
很长的off_t;



它通常是一个可移植性问题。想象一下,你想要一个包含
最大可能无符号整数的类型。

你将在C90中有这个:

typedef unsigned long largest_int_t;
<在C99上:

typedef unsigned long long large_int_t;

借助一些智能宏和#ifxxx指令,经过简单的
重新编译,在简单地重新编译项目/ lib或其他任何内容后,类型将自动更正。




Thanx为您的回复,Emmanuel。但如果是这种情况(即我们需要

最大可能

无符号整数来保持偏移量)那么为什么不宣布

unsigned long long抵消;

我相信unsigned long long应该是最高整数类型?

或者我错了?


有没有这样做的其他原因是什么?


2006年1月28日00:50:55 -0800,在comp.lang.c中,

ju ********** @ yahoo.co.in <菊********** @ yahoo.co.in>写道:


(关于typdef''ing)

Thanx为你的回复,Emmanuel。但如果是这种情况(即我们需要
最大可能的无符号整数来保持偏移量)那么为什么不声明
无符号长long偏移量;


因为那时它的大小因平台而异,而且在实际上它将是一个错误。一些实现拼写

long long如__ int64,其他人根本不支持它等。

我相信unsigned long long应该是最高可能的整数类型?
或者我错了吗?



你是对的,但长长的大小因平台而异。

平台

Mark McIntyre

-

"调试是第一次编写代码的两倍。

因此,如果你巧妙地编写代码可能,你是,

的定义,不够聪明,不能调试它。

--Brian Kernighan


- --- ==通过Newsfeeds.Com发布 - 无限制 - 无限制 - 安全的Usenet新闻== ----
http://www.newsfeeds.com 世界排名第一的新闻组服务! 120,000多个新闻组

---- =东海岸和西海岸服务器农场 - 通过加密实现全隐私= ----


I was looking at the source code of linux or open BSD. What I found
that lots of typedefs
were used. For example, consider the offset in a file. It was declared
as
off_t offset;
and off_t is typedefed as
typedef long off_t;

I wanted to know what advantage do we get by typedefs ? Why we did not
declare
offset simply as
long off_t;

Similar is the case with size of file and so on.....

Does any one has any clue of why do we use typedefs in such cases ?

Thanx for any help advance ....

解决方案

ju**********@yahoo.co.in a écrit :

I wanted to know what advantage do we get by typedefs ? Why we did not
declare
offset simply as
long off_t;



It''s often a portability issue. Imagine you want a type that hold the
biggest possible unsigned integer.

You will have this in C90:

typedef unsigned long biggest_int_t;

and that on C99:

typedef unsigned long long biggest_int_t;

With the help of some smart macros and #ifxxx directives, After a simple
recompile, the type will automagically be corrected after a simple
recompile of the project/lib or whatever.

--
A+

Emmanuel Delahaye



Emmanuel Delahaye wrote:

ju**********@yahoo.co.in a écrit :

I wanted to know what advantage do we get by typedefs ? Why we did not
declare
offset simply as
long off_t;



It''s often a portability issue. Imagine you want a type that hold the
biggest possible unsigned integer.

You will have this in C90:

typedef unsigned long biggest_int_t;

and that on C99:

typedef unsigned long long biggest_int_t;

With the help of some smart macros and #ifxxx directives, After a simple
recompile, the type will automagically be corrected after a simple
recompile of the project/lib or whatever.



Thanx for your reply, Emmanuel. But if this is the case (ie we need
the biggest possible
unsigned integer to hold offset ) then why not declare
unsigned long long offset;
I believe unsigned long long should be highest possible integer type ?
Or am I wrong ?

Is there any other reason for doing that ?


On 28 Jan 2006 00:50:55 -0800, in comp.lang.c ,
"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> wrote:

(concerning typdef''ing)

Thanx for your reply, Emmanuel. But if this is the case (ie we need
the biggest possible unsigned integer to hold offset ) then why not declare
unsigned long long offset;
Because then its size would vary from platform to platform, and on a
c90 implementation it would be an error. Some implementations spell
"long long" as "__int64", others don''t support it at all etc etc.
I believe unsigned long long should be highest possible integer type ?
Or am I wrong ?



You''re right, but the size of long long varies from platform to
platform
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----


这篇关于使用typedef的优点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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