C/C++ 中的 sizeof 运算符和 size_t 类型

简介: C/C++ 中的 sizeof 运算符和 size_t 类型

常常会有人认为 在C/C++中 sizeof 是一个函数,因为通常在使用 sizeof 的时候会带上圆括号” () “。而实际上, C/C++中的 sizeof 是一个运算符。


它的运算对象可以是具体的数据对象(例如变量名)或者数据类型,如果运算对象是一个数据类型,则必须使用圆括号将其括起来。


#include "stdio.h"
int main(void)
{
    int n = 10;
    //以下两种写法均正确
    printf("%d\n", sizeof (int));
    printf("%d\n", sizeof n);
    return 0;
}
//输出结果为:
//4
//4


在C语言的规定中,sizeof 运算符的结果是 size_t ,它是由 typedef 机制定义出来的”新”类型。


在使用 size_t 类型时,编译器会根据不同系统来替换标准类型,从而让程序有良好的可移植性。


//C/C++用 typedef 把 size_t 作为 unsigned int或 unsigned long 的别名
//size_t 的定义如下
//
// stddef.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// The C <stddef.h> Standard Library header.
//
#pragma once
#define _INC_STDDEF
#include <corecrt.h>
_CRT_BEGIN_C_HEADER
#ifdef __cplusplus
    namespace std
    {
        typedef decltype(__nullptr) nullptr_t;
    }
    using ::std::nullptr_t;
#endif
#if _CRT_FUNCTIONS_REQUIRED
    _ACRTIMP int* __cdecl _errno(void);
    #define errno (*_errno())
    _ACRTIMP errno_t __cdecl _set_errno(_In_ int _Value);
    _ACRTIMP errno_t __cdecl _get_errno(_Out_ int* _Value);
#endif // _CRT_FUNCTIONS_REQUIRED
#if defined _MSC_VER && !defined _CRT_USE_BUILTIN_OFFSETOF
    #ifdef __cplusplus
        #define offsetof(s,m) ((size_t)&reinterpret_cast<char const volatile&>((((s*)0)->m)))
    #else
        #define offsetof(s,m) ((size_t)&(((s*)0)->m))
    #endif
#else
    #define offsetof(s,m) __builtin_offsetof(s,m)
#endif
_ACRTIMP extern unsigned long  __cdecl __threadid(void);
#define _threadid (__threadid())
_ACRTIMP extern uintptr_t __cdecl __threadhandle(void);
_CRT_END_C_HEADER


我们可以简单的理解为 size_t 有如下两种定义


typedef unsigned int size_t
thpedef unsigned long size_t


我们可以用 %zd(C99标准新增)、%u、%lu 转换说明用于 printf() 显示 size_t 类型的值


#include "stdio.h"
int main(void)
{
    size_t intsize = sizeof (int);
    printf("%zd\n", sizeof (int));
    printf("%zd\n", intsize);
    printf("%u\n", intsize);
    printf("%lu\n", intsize);
    return 0;
}
//输出结果为:
//4
//4
//4
//4


相关文章
|
3天前
|
存储 程序员 C语言
c++primer plus 6 读书笔记 第四章 复合类型
c++primer plus 6 读书笔记 第四章 复合类型
|
24天前
|
C++
c++ sting类型比较大小(文本数值大小比较)
c++ sting类型比较大小(文本数值大小比较)
|
4天前
|
C++
c++语言核心技术要点,《运行时类型识别RTTI》
c++语言核心技术要点,《运行时类型识别RTTI》
13 2
|
9天前
|
编译器 C++
C++运算符的重载
C++运算符的重载
6 0
|
20天前
|
存储 安全 编译器
c++变量类型
c++变量类型
20 1
|
20天前
|
存储 编译器 程序员
c++修饰符类型
c++修饰符类型
19 1
|
20天前
|
C++
c++运算符
c++运算符
27 2
|
20天前
|
程序员 编译器 C++
c++重载运算符和重载函数
c++重载运算符和重载函数
20 1
|
22天前
|
存储 安全 程序员
C++中的四种类型转换运算符
reinterpret_cast` 则是非常危险的类型转换,仅用于二进制级别的解释,不检查安全性。`dynamic_cast` 用于类的继承层次间转换,向上转型总是安全的,向下转型时会借助 RTTI 进行安全性检查。只有当转换路径在继承链内时,转换才会成功。
11 1
|
22天前
|
程序员 C++
C++中的运算符:深入理解与应用
C++中的运算符:深入理解与应用