2.3 — Variable sizes and the sizeof operator

As you learned in the lesson 2.1 -- basic addressing and variable definition, memory on modern machines is typically organized into byte-sized units, with each unit having a unique address. Up to this point, it has been useful to think of memory as a bunch of cubbyholes or mailboxes where we can put and retrieve information, and variables as names for accessing those cubbyholes or mailboxes.

However, this analogy is not quite correct in one regard -- most variables actually take up more than 1 byte of memory. Consequently, a single variable may use 2, 4, or even 8 consecutive memory addresses. The amount of memory that a variable uses is based on its data type. Fortunately, because we typically access memory through variable names and not memory addresses, the compiler is largely able to hide the details of working with different sized variables from us.

There are several reasons it is useful to know how much memory a variable takes up.

First, the more memory a variable takes up, the more information it can hold. Because each bit can only hold a 0 or a 1, we say that bit can hold 2 possible values.

2 bits can hold 4 possible values:

bit 0 bit 1
0 0
0 1
1 0
1 1

3 bits can hold 8 possible values:

bit 0 bit 1 bit 2
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

To generalize, a variable with n bits can hold 2n (2 to the power of n, also commonly written 2^n) possible values. With an 8-bit byte, a byte can store 28 (256) possible values.

The size of the variable puts a limit on the amount of information it can store -- variables that utilize more bytes can hold a wider range of values. We will address this issue further when we get into the different types of variables.

Second, computers have a finite amount of free memory. Every time we declare a variable, a small portion of that free memory is used for as long as the variable is in existence. Because modern computers have a lot of memory, this often isn’t a problem, especially if only declaring a few variables. However, for programs that need a large amount of variables (eg. 100,000), the difference between using 1 byte and 8 byte variables can be significant.

The size of C++ basic data types

The obvious next question is “how much memory do variables of different data types take?”. You may be surprised to find that the size of a given data type is dependent on the compiler and/or the computer architecture!

C++ guarantees that the basic data types will have a minimum size:

Category Type Minimum Size Note
boolean bool 1 byte
character char 1 byte May be signed or unsigned
Always exactly 1 byte
wchar_t 1 byte
char16_t 2 bytes C++11 type
char32_t 4 bytes C++11 type
integer short 2 bytes
int 2 bytes
long 4 bytes
long long 8 bytes C99/C++11 type
floating point float 4 bytes
double 8 bytes
long double 8 bytes

However, the actual size of the variables may be different on your machine (particularly int, which is more often 4 bytes). In order to determine the size of data types on a particular machine, C++ provides an operator named sizeof. The sizeof operator is a unary operator that takes either a type or a variable, and returns its size in bytes. You can compile and run the following program to find out how large some of your data types are:

Here is the output from the author’s x64 machine (in 2015), using Visual Studio 2013:

bool:           1 bytes
char:           1 bytes
wchar_t:        2 bytes
char16_t:       2 bytes
char32_t:       4 bytes
short:          2 bytes
int:            4 bytes
long:           4 bytes
long long:      8 bytes
float:          4 bytes
double:         8 bytes
long double:    8 bytes

Your results may vary if you are using a different type of machine, or a different compiler. Note that you can not take the sizeof the void type, since it has no size (doing so will cause a compile error).

If you’re wondering what ‘\t’ is in the above program, it’s a special symbol that inserts a tab (in the example, we’re using it to align the output columns). We will cover ‘\t’ and other special symbols when we talk about the char data type.

Interestingly, the sizeof operator is one of only three operators in C++ that are a word instead of a symbol. The other two are new and delete.

You can also use the sizeof operator on a variable name:

x is 4 bytes

We’ll discuss the size of different types in the upcoming lessons, as well as a summary table at the end.

2.4 -- Integers
2.2 -- Void

114 comments to 2.3 — Variable sizes and the sizeof operator

  • Le Thu Ha

    Hi Alex,
    I went through your tutorial but could not find enough details of how can we calculate sizeof a class. I have read somewhere that it would take into account member function as well as varriable, but not static member varriable. And also it is double for virtual member function since it is written into a V-Table … Have you written those details somewhere and I missed it? Thank you very much for sharing!

    • Alex

      I don’t think I ever cover this as a discrete topic -- all the pieces are there, but I don’t connect the dots.

      Basically, the size of a class is related to:
      * The size of each non-static member variable (this includes inherited members)
      * Whether or not there’s a virtual pointer (which adds one pointer worth of size)
      * Whether there’s any padding or alignment between the members

      Any dynamic memory allocated is not included by sizeof().

      The first two items are fairly easy to manually calculate. The third is a little more tricky.

  • McSteven

    Please, When you say "the sizeof operator is one of only three operators in C++ that are a word instead of a symbol. The other two are new and delete", do you mean symbols like what?

  • darshan

    may i know sizeof(register) and register operation

  • Alan

    Hi, Alex.

    After I read "Note that you can not take the sizeof the void type, since it has no size (doing so will cause a compile error).", I tried on my Code::Blocks, and it turned out that the compiler just showed a warning, no compile errors. When I tried to print the value of sizeof(void), it showed 1. I googled it and thought that maybe it is compiler-dependent?

    • Alex

      It’s compiler dependent only in the sense that some compilers don’t follow the C++ standard like they should. Taking the sizeof(void) should be an error. Some compilers return 1 instead -- I’m not sure why, but this is non-standard behavior.

  • James Ray

    Hi Alex,

    If you CTRL+F in this article for "However, you may find on your system that the variables are larger (particularly for int, which is often 4 bytes)." I think you will see that this might be best to be omitted or moved, as you later say (after the table of minimum byte size of data types): "However, the actual size of the variables may be different on your machine."

  • Dion

    Hey Alex. No question here, just… Thanks for all of the time you’ve put into this website. Thanks for the calm and friendly manner in which you’ve answered so many often blunt, rude, arrogant, or repetitive comments. I’m learning a lot here, and I expect it’ll have a large and positive impact on my life. So, thanks.

    • Alex

      You’re welcome, thanks for letting me know you’re enjoying the site!

      I try to keep in mind that a lot of the readers here don’t speak English as their primary language. So what may seem rude or blunt to a native speaker (like me) probably isn’t intentional.

  • kundan

    i mean sizeof() operaror

    • Alex

      sizeof() isn’t used very often. It’s mainly used in two cases that I’m aware of:
      * With asserts, to ensure your data types are the size you expect them to be (e.g. if you assume anywhere that int is 4 bytes, you’d best assert that sizeof(int) >= 4)
      * With fixed arrays, as a “hacky” way to get the array’s length.

      I use it here more as a way to teach that variables use different amounts of bytes, so you can see it for yourself.

  • kundan

    where is it used?

  • #include<iostream>
    using namespace std;
    struct student
        int rollno;
        char name[10];
        float percentage;
        int subone;
    int main()
    { struct student s;

    the size of structure if size of char variable is 8, then it print 20
    & for 9,10,11,12 it print 24;

  • Matt

    Why does putting two tabs align the output colums?

  • g++ -std=c++
    (default Code::Blocks compiler)

    bool:           1 bytes
    char:           1 bytes
    wchar_t:        2 bytes
    char16_t:       2 bytes
    char32_t:       4 bytes
    short:          2 bytes
    int:            4 bytes
    long:           4 bytes
    long long:      8 bytes
    float:          4 bytes
    double:         8 bytes
    long double:    12 bytes

    The more you know!

  • Mauricio Mirabetti

    First, I guess "There are several reasons it is useful to know how much memory a variable takes up." is missing a "why" between "reasons" and "it". Although I’m not a native english speaker (Brazilian living in Brazil), I thought such an excellent tutorial deserves all the help it can get.
    Second, besides learning really well all the fundamentals of C++ (I started with Cobol early in the days, and never stopped to really learn C++ properly) with these amazing work, I laugh my a** off with some of your comments (like the one about mixing cases on variable names and to have someone’s head examined =D).
    Sorry for such a poor contribution. And thanks for all my heart. Hope I can contribute and support with your work soon.
    Best regards.


  • Ganesh

    In some books author has use <climits> it necessary to mention or its optional.

    • Alex

      The climits header defines a bunch of macro constants that indicate the minimum and maximum values for certain data types. This can be useful in some rare cases but personally I almost never use them. You could use them to do something like this:

  • Jonas

    Are variable sizes important to learn?
    I hit the wall and have gone back a few steps to try to catch up with the tutorial again, but I feel like there is a lot of stuff coming.
    I only intend to create pretty simple programs, and most like for simple games, so I don’t know if sizes are required at the moment.
    If it turns out that it is, I will of course try to learn the numbers, but I’m not that effective at that kind of learning, as in reading and remembering.

    • Alex

      It’s not as important that you remember the specific sizes as it is that you remember that variables have sizes and what the ramifications are of trying to put more data in those variables than they can hold.

      You can always look up the specific numbers if you find yourself in a situation where that’s relevant/important.

  • Michael Johnson


    I’m a grammar nazi, so it made me sad when I came across this:

    "The amount of memory that a variable uses is based on it’s data type."

    it’s -> its

  • Dyablo

    Hi, I love the guide, it’s a great help for me.
    I use code::blocks for coding and to make sure it supported C++11, I had it output a value that I had brace-initialized.
    It worked, implying support.  However, when I tried the code from this lesson with char16_t and char32_t, I get an undeclared error.  
    Any help?

  • Samuel Kurland

    I love this guide. Thanks so, so much for it.

    Just a very small nitpicky correction: "Interestingly, the sizeof operator is one of only three operators in C++ that is a word instead of a symbol" should be "Interestingly, the sizeof operator is one of only three operators in C++ that <b>are</b> words instead of symbols."

  • Rohit

    The size of wchar_t is shown as 1 byte in the table whereas it is shown as 2 byte in the example. What is the use of wchar_t if its size is 1 byte?

    • Alex

      The table indicates the minimum size for each type. wchar_t can be 1, 2, or 4 bytes. On my machine/compiler (Windows/Visual Studio), it’s 2 bytes.

      If wchar_t is 1 byte, it can still be used to hold utf-8 text. That said, wchar_t is a mess, and should generally be avoided altogether. I mention it in the lessons simply because it’s part of the standard, and so I can tell you that’s its a mess and that you should avoid it. πŸ™‚

  • hi Alex
    I can’t understand this program
    how can the result of program took 6
    how can the program be solved?
    can i use ‘long’ instead of ‘int’

    #include <iostream.h>

    int main()
        using namespace std;
        int x;
        cout<<"the size of x is:"<<sizeof(x)<<"bytes"<<endl;
        return 0;
          this result of programme  is
          x=6 but 2^4=16          16 in binary is 10000
          the size of x is : 4 bytes                  

  • Vex

    If you fail to compile demo code, like this:

        β€˜char16_t’ was not declared in this scope|
        β€˜char32_t’ was not declared in this scope|


        error C2065: ‘char16_t’ : undeclared identifier
        error C2070: ”unknown-type”: illegal sizeof operand
        error C2065: ‘char32_t’ : undeclared identifier
        error C2070: ”unknown-type”: illegal sizeof operand

    Please use following command to compile it:

        g++ -std=c++0x -o bin/Debug/2 main.cpp

  • Nahin

    Why is sizeof() considered an operator and not a built-in function?
    How can we distinguish between a function and an operator?

  • cpplx

    this line: "Because modern computers have a lot of memory, this often isn’t a problem, especially if only declaring a few variables."
    I would like you to delete it and instead suggest the would be programmers to always optimize their code.
    "A jug fills drop by drop." - The Buddha
    the future is in you hands. make it… right.

    • Alex

      Actually, best practice is to NOT prematurely optimize your code. You should code for readability, debugability, and consistency, and then optimize if and when necessary.

      In the book Computer Programming as an Art (1974), Donald Knuth (a renowned Computer Scientist) said, “The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.”

      For example, you _could_ pack every one of your boolean variables into bit flags to save memory, but for 99.99% of programs this simply isn’t necessary, and it adds a lot of complexity, degrades understanding, and increases the chance of error.

      It’s much better to use a little more memory than increase the risk of your program producing the wrong output or crashing.

  • Todd

    In your code, you follow the order given in your table except for ‘long long’ and ‘float’ (see below). I recommend switching these lines of code for consistency. Also, with 4-space tabs (as recommended), ‘long long’ needs only one tab in its print line.

    If you make this change, be sure to update your output table.

    Also, typo:

    "Note that you can not take the sizeof the void type, since is (it) has no size"

  • Tushar Girase


    struct stud
        int roll;
        char nm;
    int main()
        printf("%d",sizeof(struct stud));
        return 0;

    this program shows me the output as 8. But it shoud be the 5 brecause int takes 4 bytes and char takes 1 byte.

    • Alex

      First off, your example is using C, not C++. I’ll forgive you.

      The sizeof a struct is not necessarily equal to the sizeof the elements it contains. The reasons for this are discussed in lesson 4.7 -- structs.

  • The Questioner

    Just a simple question: Why would you use the sizeof() function?

    • Alex

      Sizeof is used mostly for reasons you’ll learn about in future lessons.

      First, you can use sizeof() to see how large integers are on your machine, so you know the largest and smallest numbers they’ll hold.

      You’ll sometimes see developers document assumptions about variable sizes like this:

      Second, later on, we’ll learn about ways to dynamically allocate memory while your program is running. If you wanted to dynamically allocate memory for 4 integers, you’ll need to know how large those integers are, so you allocate enough memory for them.

  • Niroshan

    What is the minimum size for int? I think 4 Bytes. Please Check your table.

    • Alex

      The minimum size for int (as guaranteed by the C++ standard) is 2 bytes. However, for most modern architectures, ints are 4 bytes, and thus are larger than the minimum guaranteed size.

  • Chris

    Hey Alex,

    Just a heads up, the print out from your computer is missing the "long" output. It goes from "float" straight to "long long".

  • PixelHero

    It’ll be a problem if I use the C++11 language standard to compile my programs for the rest of the tutorial? That was the only way i could make the code work, otherwise it’ll not compile at all.

    • Alex

      It’s fine if your compiler doesn’t support C++11. You’ll occasionally need to modify an example to remove C++11 specific lines (as in the case above), but I’ll generally try to keep the C++11 stuff separate so the examples will work whether your compiler supports C++11 or not.

  • Catreece

    Alright, that's weird…

    So I ran the program, curious as to what was different. I expected my values to probably have one or two higher than normal. Instead, it popped up minimum values for everything… except one. (Well, two; int was 4 instead of 2; made sense after checking the next chapter though; that’s not what bothers me, though.)

    Somehow, char32_t shows up as only 2 bytes for my machine, but in the tutorial it states that C++ architecture ensures char32_t will have an absolute minimum of 4 bytes.

    So… yeah, I dunno what's up. Is it feeding me incorrect information, or has there somehow been a change where it was found to actually be possible to compress char32_t into 2 bytes instead of 4? Something's strange regardless of the answer.

    NEVER MIND, I'd typed that all out and figured out the problem:

    In the above code, there's an error.

    What you have listed is:

        cout << "bool:tt" << sizeof(bool) << " bytes" << endl;
        cout << "char:tt" << sizeof(char) << " bytes" << endl;
        cout << "wchar_t:t" << sizeof(wchar_t) << " bytes" << endl;
        cout << "char16_t:t" << sizeof(wchar_t) << " bytes" << endl; // C++11, may not be supported by your compiler
        cout << "char32_t:t" << sizeof(wchar_t) << " bytes" << endl; // C++11, may not be supported by your compiler

    What SHOULD be listed is:

        cout << "bool:tt" << sizeof(bool) << " bytes" << endl;
        cout << "char:tt" << sizeof(char) << " bytes" << endl;
        cout << "wchar_t:t" << sizeof(wchar_t) << " bytes" << endl;
        cout << "char16_t:t" << sizeof(char16_t) << " bytes" << endl; // C++11, may not be supported by your compiler
        cout << "char32_t:t" << sizeof(char32_t) << " bytes" << endl; // C++11, may not be supported by your compiler

    It was feeding me the stats for wchar_t not char16_t or char32_t, hence why it popped up 2.

    Once I fed in the updated code, it gave back the correct 4 bytes for char32_t, and char16_t remained at 2 bytes.

    I double-checked all the others just to be on the safe side and the rest of them are good, it was just the char16_t and char32_t that were off.

    Anyway, problem solved! Yay! Shame it wasn't possible to compress char32_t down to 2 bytes, though. =P

    • Alex

      Yup, copy/paste error in the sample code. It’s fixed now. Thanks for debugging. πŸ™‚

      • Catreece

        Welcome, thanks for teaching me how to in the first place! =P

        I'm starting to think coding's a bit more of an artform than I already thought, though. Like it was already clear that it requires a lot of logic, but also a fair bit of grace. At this point, I'm starting to think it follows the rule for art:

        Art is never "finished", it simply runs out of time, resources or patience.

        Debugging seems to be a similar endeavour - you'll never get 100% of it cleared out on really complex programs (Maya, Windows, whatever), which explains why they're always coming out with new patches to fix stuff.

        I kind of knew before that changing anything in a program could inadvertently break something else in the process if you didn't especially carefully comment the hell out of everything, hence why so many games break something during a patch (LoL and WoW are especially notorious for such). I'm starting to see why it happens so often now, though, and even good commenting isn't always enough to completely avoid the damage.

        I suppose that's why alpha testers are so important though, and why wide-scale beta tests are essential. A million people trying to break something are simply vastly more effective at finding ways to do so casually, than a handful of dedicated testers that are actively trying to break something.

        Anyway, thanksies for the tutorial as normal. I'd almost suggest the average person should try to go through the first 2-3 chapters just to get a feel for how many problems there can be when writing a program, so that they're a little more understanding of the issues faced. =P

  • FrostByte

        cout << "char16_t:\t" << sizeof(wchar_t) << " bytes" << endl; // C++11, may not be supported by your compiler
        cout << "char32_t:\t" << sizeof(wchar_t) << " bytes" << endl; // C++11, may not be supported by your compiler
        cout << "char16_t:\t" << sizeof(char16_t) << " bytes" << endl; // C++11, may not be supported by your compiler
        cout << "char32_t:\t" << sizeof(char32_t) << " bytes" << endl; // C++11, may not be supported by your compiler

    Otherwise it prints out wchar_t size instead of char16_t and char32_t!

  • machello

    Hi Alex!

    I must say you have highly systematic tutorials which are very pedagogical written!

    I just have one remark which is worth to mention.
    If trying to determine the size of a structure or in general any object, you probably
    may not get the amount of bytes used by the object as you probably expected.

    struct Something
    int nX, nY, nZ;
    double dX;

    Something sSomeStuff;

    You expect (64bit, gcc 4.7.2): sizeof( sSomeStuff ) == 20 (= 3*4 + 8)
    (i.e. sizeof(int) == 3, sizeof(double) == 8 ).
    The object uses 20 bytes of memory or at least
    that must is allocated for each variable in the structure. However you won’t that value!
    Instead you get: sizeof( sSomeStuff ) == 24.

    I won’t explain in details (internet is used for this purpose) but it is related with
    how CPU can most efficienty collect variables (blocks) or more techical, is related with
    data structure alignment or internal padding.

    In the example, the higest variable size (double) is sizeof( double ) == 8. Total
    effective size of the structure (without padding) is 20. Structure block size must
    be (or should, for most systems) multiple of the highest variable size in that structure,
    in the example, multiple of 8: 8, 16, 24, 32,…
    Optimal structure size is not 16, which is to small,
    but 24, which is higher than effective structure size.

    With proper order of type declarations, additional padding bytes could be minimized.

    With regards,

  • bacia

    Using long doubles I made a program which can compute factorials up to 170!, more than my TI-831!

  • jimbo

    It’s ok I just worked it out. one bit can store two values i.e a 0 or a 1 i.e 2^1 2*1=2. 2 bits can store 4 different values i.e 2^2 2*2= 4 and 3 bits can store 8 different values i.e 2^3 2*2*2= 8.

    One more thing..just wondering what to the nth power means? is it like 2^9 or 2*2*2*2*2*2*2*2*2? Many thanks in advance.

    • tsdrifter

      n doesn’t stand for 9, it just stands for any integer. what he is saying is if you have a given number of bits, you can store 2^(number of bits). if you had nine bits, then n would equal 9, but this is not always the case.

    • rameye

      Since ^ is being so freely used here in the comments, I must mention to be careful using ^ as an operator in C++ expressions.

      C++ has NO built-in operator for exponentiation. The ^ operator in C++ is a binary (operating on two values) operator performing a bitwise XOR.

      For example what does 2^3 resolve to in C++?

      int x = 2^3;

      x will not contain the value 8, it will contain the value 1

      Why is this?

      Assuming 8 bit wide integers this is what happened:

      00000010 ^
      00000001 <== bitwise XOR of the two integers results in value of 1

  • jimbo

    First of all thanks Alex for this amazing tutorial…your awesome!!

    I was just wandering if one bit can store a 0 or a 1 so two values and two bits can store 4 different values i.e 0101. How come it says 3 bits can store 8 values i.e 01010101? Surely 3 bits would store 6 different values if 1 bit stores 2 different values. 3*2 = 6. If I am missing the point can someone please try and explain where I am going wrong!?
    Many thanks in advance.

  • sanjeev_e

    Hi Alex/Members,

    Is there any other way to find the size of a variable without using sizeof(operator).

    I have seen in some of the websites that the below way we can find the same.

    int i = 1;

    size_t size1 = (char*)(&i+1)-(char*)(&i);

    size_t size2 = (int*)(&i+1)-(int*)(&i);


    Output: 4 1

    Why it is varying here by typecasting with different datatypes?

    I want to know where it is restricted to use sizeof() operator anywhere? and also whether the above code internally uses sizeof() operator like

    size1 = address-diff/sizeof(int); size2 = address-diff/sizeof(char);

    Please clarify.


    • rameye

      Any time you use mathematical operators with pointer types, you then are working with pointer arithmetic, and the expressions will evaluate differently depending on the size of the type pointed to.

      int i;
      size_t size1 = &i+1; // size1 is assigned address of next byte in storage space of the integer i
      size_t size2 = (int*)(&i+1); // size2 is assigned address of the next following integer, perhaps in an array.

      In the case of the size2 assignment in your posting, pointer arithmetic will resolve the size of the type, by taking the difference of the addresses of two identical data types that are adjacent in memory.

  • daksh

    bool: 1 bytes
    char: 1 bytes
    wchar_t: 2 bytes
    short: 2 bytes
    int: 4 bytes
    long: 4 bytes
    float: 4 bytes
    double: 8 bytes
    long double: 8 bytes

    Your results may vary if you are using a different type of machine, or a different compiler.

    I can understand that it depends on the machine.. But how does it depend on the compiler??

    • rameye

      Most modern compilers have options you can specify on the command line to target different architectures.

      There is a lot of information in the g++ documentation about this.

  • kemawalker

    I am on a MAC (64bit) so my values are significantly larger for 4 of the types.

    QUESTION - how do you manage the risk that you might code for too large a type that won’t run on other machines? In other words, if I code using “long double” which is 16 bytes on my machine, but only 8 bytes on yours, will there be an issue when the program runs or no?

    • Alex

      This is a challenging question with no easy answer.

      If you’re writing for portability, the best thing to do is assume that variables are only as large as their guaranteed minimum sizes and avoid the problem in the first place.

      If you do need to assume long double is 16 bytes for your code, you could use assert() to “document” that:

      That way anybody who compiles the code on an architecture or compiler that is using a different size would be aware of the issue.

  • This tutorial is so easy. It makes learning programing so easy. The problem comes once you get out of C++ and try to understand msdn help. These guys from Microsoft are from outer space. Just try looking up some of the things you learn here and see if you can get anything that explains it in common english. I could sure use a tutorial on how to use Microsoft help, once you get to the help you need. Sometimes they just go round and round.

  • Anon

    So does it really matter what variable type you use? Dont get me wrong i want to use the right one, but it seems a task to remember all of the maximum values etc.

    is there a trick to it?

    There’s an awful lot to choose from.

    • PReinie

      It depends on the use or application of the code and your engineering design. (You can’t put 5# of @&*!*^ in a 1# bag.) As Alex said above “However, for programs that need a large amount of variables (eg. 100,000), the difference between using 1 byte and 8 byte variables can be significant.” that’s an 8-times increase of memory.

      If you use the maximum word size for each piece of memory, your “product” may cost more because you need more memory to hold or run your program. The additional memory may also require larger circuit boards to hold the memory chips and power for the boards/memory which increases the weight and may require a fan (which takes power) to cool the components. This might be important when go to market or have to carry it to a space station, a battlefield, hiking up a mountain or as a cell phone or iPod.

      If you’re just providing code for existing computers it may not be that big a deal.

      (Sorry for the long winded explanaition. I’ve programmed to the bit level in assembly and in C for limited memory machines.)

  • Ranjan

    what the resone behind it, why you can’t overload the size of operator ?


    • Why would you want to? πŸ™‚

      Stroustrup says here:

      Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:

      X a[10];
      X* p = &a[3];
      X* q = &a[3];
      p++; // p points to a[4]
      // thus the integer value of p must be
      // sizeof(X) larger than the integer value of q

      Thus, sizeof(X) could not be given a new and different meaning by the programmer without violating basic language rules.

  • Adam

    What do you mean when you use ^ and n in your equations? Are these standard operators and variables?

  • Julian

    Wow, imagine how many variables I could store in a kilobyte πŸ˜€

    I know that years ago when they used to use punchtape in stuff like CNC machines, having a strip of tape that was over 12 kilobytes of information was very impracticle πŸ˜›

    Now that I think about it, punchtape was just a lot like bytes, where each row on the tape would have 8 circles, some of which were punched out (which I guess would be the equivalent to a bit with value 1) and the computer would read the values off it… At least I think that’s how it worked ;S

    • rameye

      Back in the late 1970s we had an air show at the Air Force base I was stationed at. We had a paper tape punch machine set up so that attendees could type in their name or whatever and have it punched out on the tape as the actual letters, not the binary. Was a hit.

  • Mitul Golakiya

    My int takes 2 bytes.
    I am working with tourbo C .
    My PC is 32-bit.

  • Syed

    Hi, I would like to know what is the sizeof(long double); ? In this tutorial it is mentioned 8 bytes. Where as i tried in Dev C++ compiler it is giving 12.


    • The size of a long double can vary from machine to machine. On most machines, it is either 8 or 12. The only way to know for sure is to use sizeof(long double) just as you have.

  • vader347

    my long double takes 12 bytes

  • Nikki

    “On most 32-bit machines …. ”
    I am not sure if this is the right place to ask this question .
    What is a “32-bit machine ” ?

    • Computers work by moving binary digits (bits) around. However, most computers do not work with individual bits -- rather, they move data around in chunks. This chunk size is called a “word”. Typically, when we speak of the bit-ness of a machine, we speak of the size of a word. Thus, a 32-bit machine has a 32-bit word size, which means it moves information around 32-bits at a time.

      Typically, modern computers use one word to address memory. With a 32-bit word, this means there are about 2^32 (4 billion) unique memory addresses that can be addressed. This is why 32-bit machines generally can’t make use of more than 4GB of memory.

      • Frederik

        The amount of memory a machine can address has nothing to do with the “bitness” of the machine -- in fact there is no real consensus on what it means to be of a certain “bitness”.

        Some 32 bits machines (such as the Pentium Pro and later 32 bit x86s) can address more than 4 GiB, but they are still only considered 32 bit computers.

        I (personally) only consider a machine to be an n bit machine, if it is able to (at least) address up to 2^n individual bytes, is able to hold at least n bits in all registers and can do all operations on n bit (or larger) registers.

      • JD

        So, if I understand this correctly, the computer moves data around in 32-bit chunks (4 bytes), but in my C++ program I can assign variables to a single byte of memory. This seems contradictory to me.

        I guess my question boils down to: Say I have a program that uses a large number of variables, so space matters. One byte will suffice for the data I need to carry so to save space I assign them as chars (1 byte). But, if the smallest “chunk” that the computer passes around is 4 bytes, does this actually save any space? Or would this effectively use the same amount of memory as if I made my variables ints (4 bytes, or one full word)?

        This is a somewhat subtle question, so let me know if I’m not being clear.

        PS - Thanks for a clear, organized, and well-written tutorial! I’ve really enjoyed it so far.

        • Alex

          Yes, using 1-byte variables saves space. Modern architectures generally have memory that is byte-addressable, meaning you can read/write memory one byte at a time. x86 architecture computers can move different size “chunks” around (1 byte, 2 byte, 4 bytes, etc…).

          Generally you won’t have to worry about how the computer internally accesses memory and moves data around. Your compiler should be smart enough to use the best set of assembly instructions for whatever task you’re throwing at it.

      • zingmars

        It would be easier to explain the whole thing using the architecture name x86(x86_64) rather than calling them ’32-bit’ machines. Confuses people.

  • Abhishek

    Never heard of wchar_t before……is that a new data type?What kind of data does it hold?

    • There’s more information about wchar_t on wikipedia. In short, it was meant to be used to hold “wide characters” (eg. those that take more than 8 bits to represent). However, the size varies depending on platform (and can be as small as 8 bits), so I’m not sure I see the practical use.

      Edit: To be clear, I’m not sure I see the practical use of using wchar_t instead of a variable that’s guaranteed to be at least 16 bits (e.g. char16_t).

      • chris

        Pocket PC/Windows Mobile only uses wide characters. Beyond that the practical use is for foreign languages. Some languages (like Chinese) have a lot more than 128 characters.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter