C/C++中的sizeof運(yùn)算符和size_t類型的詳解
sizeof的作用
sizeof是c的運(yùn)算符之一,用于獲取操作數(shù)被分配的內(nèi)存空間,以字節(jié)單位表示.
這里指的操作數(shù),可以是變量,也可以是數(shù)據(jù)類型,如int,float等.所以就可以通過它來獲取本地c庫定義的基本類型的范圍。
sizeof的使用
1.對于一般變量,形式2種:sizeof a 或 sizeof(a);
2.對于數(shù)據(jù)類型,必須使用帶括號的方式,如sizeof(int).
size_t的說明
size_t是標(biāo)準(zhǔn)C庫中定義的,應(yīng)為unsigned int,在64位系統(tǒng)中為 long unsigned int。
sizeof返回的必定是無符號整形,在標(biāo)準(zhǔn)c中通過typedef將返回值類型定義為size_t.
若用printf輸出size_t類型時(shí),C99中定義格式符%zd;若編譯器不支持可以嘗試%u或%lu.
sizeof和size_t
常常會(huì)有人認(rèn)為 在C/C++中 sizeof 是一個(gè)函數(shù),因?yàn)橥ǔT谑褂?sizeof 的時(shí)候會(huì)帶上圓括號” () “。而實(shí)際上, C/C++中的sizeof 是一個(gè)運(yùn)算符。
它的運(yùn)算對象可以是具體的數(shù)據(jù)對象(例如變量名)或者數(shù)據(jù)類型,如果運(yùn)算對象是一個(gè)數(shù)據(jù)類型,則必須使用圓括號將其括起來。
#include "stdio.h" int main(void) { int n = 10; //以下兩種寫法均正確 printf("%d\n", sizeof (int)); printf("%d\n", sizeof n); return 0; } //輸出結(jié)果為: //4 //412345678910111213141516
在C語言的規(guī)定中,sizeof 運(yùn)算符的結(jié)果是 size_t ,它是由 typedef 機(jī)制定義出來的”新”類型。
在使用 size_t 類型時(shí),編譯器會(huì)根據(jù)不同系統(tǒng)來替換標(biāo)準(zhǔn)類型,從而讓程序有良好的可移植性。
//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_t thpedef unsigned long size_t12
我們可以用 %zd(C99標(biāo)準(zhǔn)新增)、%u、%lu 轉(zhuǎn)換說明用于 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; } //輸出結(jié)果為: //4 //4 //4 //4
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽多個(gè)端口
這篇文章主要介紹了c++ 單線程實(shí)現(xiàn)同時(shí)監(jiān)聽多個(gè)端口的方法,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下2021-03-03C++實(shí)現(xiàn)一個(gè)簡易版的事件(Event)的示例代碼
之前在?windows系統(tǒng)中開發(fā)應(yīng)用時(shí),?遇到需要進(jìn)行線程同步的時(shí)候幾乎都是使用的事件內(nèi)核對象?Event。本文為大家整理了C++實(shí)現(xiàn)一個(gè)簡易版的事件(Event)的相關(guān)資料,需要的可以參考一下2022-11-11C++實(shí)現(xiàn)LeetCode(79.詞語搜索)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(79.詞語搜索),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07