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

sizeof的作用

sizeof是c的运算符之一,用于获取操作数被分配的内存空间,以字节单位表示.

这里指的操作数,可以是变量,也可以是数据类型,如int,float等.所以就可以通过它来获取本地c库定义的基本类型的范围。

sizeof的使用

1.对于一般变量,形式2种:sizeof a 或 sizeof(a);

2.对于数据类型,必须使用带括号的方式,如sizeof(int).

size_t的说明

size_t是标准C库中定义的,应为unsigned int,在64位系统中为 long unsigned int。

sizeof返回的必定是无符号整形,在标准c中通过typedef将返回值类型定义为size_t.

若用printf输出size_t类型时,C99中定义格式符%zd;若编译器不支持可以尝试%u或%lu.

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//412345678910111213141516

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

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

typedef unsigned int size_tthpedef unsigned long size_t12

我们可以用 %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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

我们摇摇头说,困难其实没什么大不了。

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

相关文章:

你感兴趣的文章:

标签云: