ALERT

[FREE DEMO] Deploy Your Enterprise Cloud in Minutes

Sizeof Operator

Definition - What does Sizeof Operator mean?

Sizeof operator, in C#, is an operator used to determine the size (in bytes) of an unmanaged type that is not a reference type.

While developing applications that involve dynamic memory allocation, it is very common to find the memory allocated to a type. It is very unsafe to assume the size of a type and use the hard-coded value in the application, as this may break the application when ported to different systems. The sizeof operator is used in such cases to find the size of a compound data type like a struct. However, it can only be used to compute the byte size of value types and not for reference types due to the virtualized type layout system of the .NET framework.

The sizeof operator helps in memory allocation for data structures that are passed out of managed application to unmanaged code like Interop, custom serialization, etc. Increment and decrement operators, which operate on pointers, use the sizeof operator internally to increment or decrement the address contained in a pointer variable by a value equal to the number of bytes occupied by the type of pointer. The sizeof operator helps improve code readability.

Techopedia explains Sizeof Operator

The sizeof operator is a unary operator that takes a singe parameter and is used with the keyword "sizeof". In contrast to C++, the sizeof operator is a compile-time construct, so the parameter has to be a closed type known during compilation, not variables. The parameter can also be an enum, a pointer or a user-defined struct without any field or reference type properties. For certain predefined types, the sizeof operator returns a constant value, while its use with remaining types results in values based on implementation.

For example, when the sizeof operator is executed with integer (int) as a parameter, it always returns the number four to indicate that a variable of integer type occupies four bytes of memory.

When the sizeof operator is applied to a struct type operand, it fetches the total number of bytes occupied by a struct, which includes the padding bytes used for its alignment internally. The guidelines to follow while using the sizeof operator with struct include:
  • It should be called within an unsafe block
  • The struct variable must not contain a member of reference type
  • The struct must not be a generic value type
This definition was written in the context of C#

Techopedia Deals

Connect with us

Techopedia on Linkedin
Techopedia on Linkedin
Tweat cdn.techopedia.com
"Techopedia" on Twitter


'@Techopedia'
Sign up for Techopedia's Free Newsletter!

Email Newsletter

Join thousands of others with our weekly newsletter

Resources
The 4th Era of IT Infrastructure: Superconverged Systems
The 4th Era of IT Infrastructure: Superconverged Systems:
Learn the benefits and limitations of the 3 generations of IT infrastructure – siloed, converged and hyperconverged – and discover how the 4th...
Approaches and Benefits of Network Virtualization
Approaches and Benefits of Network Virtualization:
Businesses today aspire to achieve a software-defined datacenter (SDDC) to enhance business agility and reduce operational complexity. However, the...
Free E-Book: Public Cloud Guide
Free E-Book: Public Cloud Guide:
This white paper is for leaders of Operations, Engineering, or Infrastructure teams who are creating or executing an IT roadmap.
Free Tool: Virtual Health Monitor
Free Tool: Virtual Health Monitor:
Virtual Health Monitor is a free virtualization monitoring and reporting tool for VMware, Hyper-V, RHEV, and XenServer environments.
Free 30 Day Trial – Turbonomic
Free 30 Day Trial – Turbonomic:
Turbonomic delivers an autonomic platform where virtual and cloud environments self-manage in real-time to assure application performance.