16

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • You are missing a ' in your char. – Konrad Jul 08 '10 at 11:50
  • 8
    @Tyler McHenry Timing is little off the mark for me at 0.08 secs. – DumbCoder Jul 08 '10 at 12:02
  • 8
    @davit: if you're going to learn a programming language you should really try reading a decent introductory book on the language to get the basics, not just leave it to trial, error, guesswork and endless questions on SO. – Paul R Jul 08 '10 at 12:19
  • 20
    It always amuses me when I Google for a question, and the first link is to SO where someone's being berated for asking rather than googling! :) – Chap Jul 26 '13 at 01:21
  • The answer on [What does sizeof do?](http://stackoverflow.com/a/3203180/919057) should explain what is happening. – SnoringFrog Nov 03 '13 at 21:48
  • 1
    I'm voting to close this question as off-topic because this question shows no effort at all. – gsamaras Sep 13 '16 at 00:24

4 Answers4

57

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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • int means that if variable t is given t=100 than sizeof(t)= 1? –  Jul 08 '10 at 11:53
  • `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. – Tyler McHenry Jul 08 '10 at 11:56
  • 1
    It shows 4 because an `int` typically uses up 4 bytes. `sizeof` has nothing to do with the value stored in the variable. – Peter Alexander Jul 08 '10 at 11:57
  • 2
    nit: technically, sizeof does not report the size in bytes, but whatever unit necessary so that sizeof(char) == 1. The is nearly universally bytes, but in theory at least, not necessarily. – James Curran Jul 08 '10 at 14:44
  • 1
    From the standard: "The sizeof operator yields the number of bytes in the object representation of its operand." However, you are right that byte in the standard isn't explicitly defined as 8-bits as it usually is. – Peter Alexander Jul 09 '10 at 06:58
  • Your first assert is wrong. ` &(a[0])` is an int pointer so pointer arithmetic will make it equal to `a[4]`. – user1794469 Sep 08 '16 at 00:35
  • How efficient is the `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);`? – Aaron Franke Jan 26 '19 at 04:28
10

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.

Derrick Turk
  • 4,246
  • 1
  • 27
  • 27
  • A byte isn't necessarily the "smallest addressable unit for a given architecture". On some CDC Cyber computers, the smallest addressable unit is a 16-bit word (Address 0 is 16-bits, address 1 is a distinct 16-bits), and they refer to the two halves as bytes. – James Curran Jul 08 '10 at 14:50
  • @James: Right: my point was that "byte" is an inherently ambiguous term, and no matter how you define it, somebody somewhere will use it differently. Interesting about the CDCs. – Derrick Turk Jul 08 '10 at 16:02
  • Most people will agree that a byte is 8 bits, and while there used to be some confusion, it's standardized nowadays: IEC 80000-13. In contrast, the smallest addressable unit in a machine is often called a word, and it is machine-dependent. There are 16-, 32-, 64-bit architectures, but even in those contexts, a byte will still be 8 bit. – Zane Nov 07 '13 at 11:59
6

sizeof() returns the size of the argument passed to it. sizeof() cpp reference

PeterK
  • 6,287
  • 5
  • 50
  • 86
  • How efficient is the `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);`? – Aaron Franke Jan 26 '19 at 04:29
  • sizeof() is compile time, so its the best performance you can get, since at runtime thats just a constant number in your code. – PeterK Jan 26 '19 at 11:55
0

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++

Sina Raoufi
  • 54
  • 2
  • 6