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

2020-01-06 19:38:36王振洲


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

在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_HEADER15161718192021222324252627282930313233343536373839404142434445464748495051

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


typedef unsigned int size_t
thpedef unsigned long size_t12

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


#include "stdio.h"
int main(void)
{
  size_t intsize = sizeof (int);
  printf("%zdn", sizeof (int));
  printf("%zdn", intsize);
  printf("%un", intsize);
  printf("%lun", intsize);
  return 0;
}
//输出结果为:
//4
//4
//4
//4

总结