The sizeof operator and the size_t type detail in C and C++

  • 2020-06-12 10:05:33
  • OfStack

The role of sizeof

sizeof is the operator 1 of c and is used to get the memory space allocated for the operand, expressed in bytes.

The operands can be variables or data types, such as int,float, etc. This allows you to get a range of basic types defined by the local c library.

The use of sizeof

1. For a general variable, there are two forms :sizeof a or sizeof(a);

2. For data types, you must use parentheses, such as sizeof(int).

The specification of size_t

size_t is defined in the standard C library as unsigned int and long unsigned int in 64-bit systems.

sizeof returns what must be an unsigned plastic. In standard c, the return value type is defined by typedef as size_t.

When output type size_t with printf, format %zd is defined in C99. Try %u or %lu if the compiler does not support it.

sizeof and size_t

It is often argued that sizeof is a function in C/C++ because sizeof is usually used with the parenthesis "()". In fact, sizeof in C/C++ is an operator.

Its operands can be concrete data objects (such as variable names) or data types. If an operand is a data type, it must be enclosed in parentheses.


#include "stdio.h"
int main(void)
{
  int n = 10;
  // Either of the following is correct 
  printf("%d\n", sizeof (int));
  printf("%d\n", sizeof n);
  return 0;
}
// The output result is: 
//4
//412345678910111213141516

In the C language, the result of the sizeof operator is size_t, which is a "new" type defined by the typedef mechanism.

When using the size_t type, the compiler replaces the standard type for different systems, making the program portable.


//C/C++ with  typedef  the  size_t  As a  unsigned int or  unsigned long  The alias 
//size_t  Is defined as follows 
// 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 has two definitions


typedef unsigned int size_t
thpedef unsigned long size_t12

We can use the %zd(new to the C99 standard), %u, %lu conversion specification for printf() to display size_t type values


#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;
}
// The output result is: 
//4
//4
//4
//4

conclusion


Related articles: