Which data type to use inorder to store value in optimum way

This question already has an answer here:

  • What does the C++ standard state the size of int, long type to be? 24 answers

  • An unsigned short will consume half the memory of a signed int (2 bytes vs 4 bytes). Because the processor often needs to work on whole integers the execution might be slightly slower since it needs to unpack the shorts.

    In your case you could also use a signed short, it ranges from roughly -32K to +32K.


    depends : lets say you want to create an array of size 1000000

    int foo[1000000] ={};//4000000 byte
    unsigned short foo[1000000]={}; //2000000 byte 
    //you save 2 MB of memory
    

    but

    int foo;
    unsigned short foo;
    //that's two bytes not much really  even when multiplied by a dozen of variables... 
    
    链接地址: http://www.djcxy.com/p/40408.html

    上一篇: C“int”的大小是2个字节还是4个字节?

    下一篇: 用哪种数据类型来以最佳方式存储数值