What is the main function of sizeof
(I am new to C++). For instance
int k=7;
char t='Z';
What do sizeof (k)
or sizeof (int)
and sizeof (char)
mean?
What is the main function of sizeof
(I am new to C++). For instance
int k=7;
char t='Z';
What do sizeof (k)
or sizeof (int)
and sizeof (char)
mean?
sizeof(x)
returns the amount of memory (in bytes) that the variable or type x
occupies. It has nothing to do with the value of the variable.
For example, if you have an array of some arbitrary type T
then the distance between elements of that array is exactly sizeof(T)
.
int a[10];
assert(&(a[0]) + sizeof(int) == &(a[1]));
When used on a variable, it is equivalent to using it on the type of that variable:
T x;
assert(sizeof(T) == sizeof(x));
As a rule-of-thumb, it is best to use the variable name where possible, just in case the type changes:
int x;
std::cout << "x uses " << sizeof(x) << " bytes." << std::endl
// If x is changed to a char, then the statement doesn't need to be changed.
// If we used sizeof(int) instead, we would need to change 2 lines of code
// instead of one.
When used on user-defined types, sizeof
still returns the amount of memory used by instances of that type, but it's worth pointing out that this does not necessary equal the sum of its members.
struct Foo { int a; char b; };
While sizeof(int) + sizeof(char)
is typically 5
, on many machines, sizeof(Foo)
may be 8
because the compiler needs to pad out the structure so that it lies on 4 byte boundaries. This is not always the case, and it's quite possible that on your machine sizeof(Foo)
will be 5, but you can't depend on it.
sizeof
has nothing to do with the value of the variable. It has to do with the type. It is telling you that on your system, all variables of type int
require 4 bytes of storage. –
Hufford int
typically uses up 4 bytes. sizeof
has nothing to do with the value stored in the variable. –
Creolacreole a[4]
. –
Bellerophon sizeof()
operation? If I call sizeof()
in hundreds of places, is that slow, and should I store it in a variable or something? Something like int LONG_SIZE = sizeof(long);
? –
Contestation To add to Peter Alexander's answer: sizeof yields the size of a value or type in multiples of the size of a char
---char
being defined as the smallest unit of memory addressable (by C or C++) for a given architecture (and, in C++ at least, at least 8 bits in size according to the standard). This is what's generally meant by "bytes" (smallest addressable unit for a given architecture) but it never hurts to clarify, and there are occasionally questions about the variability of sizeof (char)
, which is of course always 1
.
sizeof() returns the size of the argument passed to it. sizeof() cpp reference
sizeof()
operation? If I call sizeof()
in hundreds of places, is that slow, and should I store it in a variable or something? Something like int LONG_SIZE = sizeof(long);
? –
Contestation sizeof
is a compile time unary operator that returns size of data type.
For example:
sizeof(int)
will return the size of int
in byte.
Also remember that type sizes are platform dependent.
Check this page for more details: sizeof in C/C++
© 2022 - 2024 — McMap. All rights reserved.