简体   繁体   English

“#define __SIZE_T”和“typedef unsigned int size_t”

[英]“#define __SIZE_T” and “typedef unsigned int size_t”

I've come across the following declaration of "size_t" a few times now, but don't really know what this means:我现在几次遇到以下“size_t”声明,但真的不知道这是什么意思:

#ifndef __SIZE_T
#define __SIZE_T
typedef unsigned int size_t;
#endif

What exactly is going on here?这里到底发生了什么? What is #define __SIZE_T doing? #define __SIZE_T 在做什么? Isn't the typedef alone enough to get the job done?仅 typedef 还不足以完成工作吗?

Identifiers and macros with double underscore or one underscore followed by a capital letter are reserved for the compiler.带有双下划线或一个下划线后跟一个大写字母的标识符和宏是为编译器保留的。 So __SIZE_T is most likely something internal in the specific compiler.所以__SIZE_T很可能是特定编译器内部的东西。 (I think for example gcc/glibc internally uses something called __SIZE_TYPE__ .) (我认为例如 gcc/glibc 在内部使用了一些叫做__SIZE_TYPE__的东西。)

The #ifndef... #define is simply conditional compilation (sometimes called sloppily called "compiler switch", which is also a term used for compiler arguments). #ifndef... #define只是简单的条件编译(有时被草率地称为“编译器开关”,这也是用于编译器参数的术语)。 It checks if __SIZE_T has already been defined or not.它检查__SIZE_T是否已经定义。 This has to be a separate macro, since you can't do pre-processor checks on types.这必须是一个单独的宏,因为您不能对类型进行预处理器检查。 Something like #ifndef size_t won't work if size_t is a type.如果size_t是一种类型,则像#ifndef size_t这样的东西将不起作用。

So this seems to be a compiler's internal definition of the standard type size_t for a given compiler and target.所以这似乎是编译器对给定编译器和目标的标准类型size_t的内部定义。 Not something the application programmer need to care or worry about.这不是应用程序程序员需要关心或担心的事情。

Typically you would find size_t inside stddef.h , same thing goes with NULL .通常你会在stddef.h中找到size_tNULL However, the standard (7.19 and 7.21) states that stdio.h should also define these two.然而,标准(7.19 和 7.21)规定stdio.h也应该定义这两个。 Hence the need for the macro, in case both stddef.h and stdio.h are included in the same program.因此需要宏,以防stddef.hstdio.h都包含在同一个程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM