溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C/C++中sizeof運(yùn)算符和size_t類型的示例分析

發(fā)布時(shí)間:2021-06-18 09:50:47 來(lái)源:億速云 閱讀:118 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹C/C++中sizeof運(yùn)算符和size_t類型的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

sizeof的作用

sizeof是c的運(yùn)算符之一,用于獲取操作數(shù)被分配的內(nèi)存空間,以字節(jié)單位表示.

這里指的操作數(shù),可以是變量,也可以是數(shù)據(jù)類型,如int,float等.所以就可以通過(guò)它來(lái)獲取本地c庫(kù)定義的基本類型的范圍。

sizeof的使用

1.對(duì)于一般變量,形式2種:sizeof a 或 sizeof(a);

2.對(duì)于數(shù)據(jù)類型,必須使用帶括號(hào)的方式,如sizeof(int).

size_t的說(shuō)明

size_t是標(biāo)準(zhǔn)C庫(kù)中定義的,應(yīng)為unsigned int,在64位系統(tǒng)中為 long unsigned int。

sizeof返回的必定是無(wú)符號(hào)整形,在標(biāo)準(zhǔn)c中通過(guò)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ì)帶上圓括號(hào)” () “。而實(shí)際上, C/C++中的sizeof 是一個(gè)運(yùn)算符。

它的運(yùn)算對(duì)象可以是具體的數(shù)據(jù)對(duì)象(例如變量名)或者數(shù)據(jù)類型,如果運(yùn)算對(duì)象是一個(gè)數(shù)據(jù)類型,則必須使用圓括號(hào)將其括起來(lái)。

#include "stdio.h"
int main(void)
{
  int n = 10;
  //以下兩種寫(xiě)法均正確
  printf("%d\n", sizeof (int));
  printf("%d\n", sizeof n);
  return 0;
}
//輸出結(jié)果為:
//4
//412345678910111213141516

在C語(yǔ)言的規(guī)定中,sizeof 運(yùn)算符的結(jié)果是 size_t ,它是由 typedef 機(jī)制定義出來(lái)的”新”類型。

在使用 size_t 類型時(shí),編譯器會(huì)根據(jù)不同系統(tǒng)來(lái)替換標(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

我們可以簡(jiǎn)單的理解為size_t 有如下兩種定義

typedef unsigned int size_t
thpedef unsigned long size_t12

我們可以用 %zd(C99標(biāo)準(zhǔn)新增)、%u、%lu 轉(zhuǎn)換說(shuō)明用于 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

以上是“C/C++中sizeof運(yùn)算符和size_t類型的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI