Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. What is the size of an integer?

What is the size of an integer?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comdata-structures
18 Posts 10 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vikas amin
    wrote on last edited by
    #1

    It might sound strange but as far as i know integer is 2 bytes . But in my code i declared such a simple array int arr[2][3][3] = { { {2000, 4,1}, {7, 8,1}, {3, 9,1} }, { {71, 61,11}, {31, 41,11}, {51, 91,11} } }; Why the hell do it store a integer as 4 byte in VC++ it a simple console app. :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

    M O S 3 Replies Last reply
    0
    • V vikas amin

      It might sound strange but as far as i know integer is 2 bytes . But in my code i declared such a simple array int arr[2][3][3] = { { {2000, 4,1}, {7, 8,1}, {3, 9,1} }, { {71, 61,11}, {31, 41,11}, {51, 91,11} } }; Why the hell do it store a integer as 4 byte in VC++ it a simple console app. :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

      M Offline
      M Offline
      MF
      wrote on last edited by
      #2

      In Win32, sizeof(int) is 4 bytes.

      1 Reply Last reply
      0
      • V vikas amin

        It might sound strange but as far as i know integer is 2 bytes . But in my code i declared such a simple array int arr[2][3][3] = { { {2000, 4,1}, {7, 8,1}, {3, 9,1} }, { {71, 61,11}, {31, 41,11}, {51, 91,11} } }; Why the hell do it store a integer as 4 byte in VC++ it a simple console app. :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

        O Offline
        O Offline
        Owner drawn
        wrote on last edited by
        #3

        See this... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vccelng4/html/elconTypeint.asp[^]

        Jesus Loves You and Me :)

        --Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord

        1 Reply Last reply
        0
        • V vikas amin

          It might sound strange but as far as i know integer is 2 bytes . But in my code i declared such a simple array int arr[2][3][3] = { { {2000, 4,1}, {7, 8,1}, {3, 9,1} }, { {71, 61,11}, {31, 41,11}, {51, 91,11} } }; Why the hell do it store a integer as 4 byte in VC++ it a simple console app. :doh: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

          S Offline
          S Offline
          sunit5
          wrote on last edited by
          #4

          size of int is OS dependent never say die

          Z V 2 Replies Last reply
          0
          • S sunit5

            size of int is OS dependent never say die

            Z Offline
            Z Offline
            Zdeslav Vojkovic
            wrote on last edited by
            #5

            actually, it is not OS dependent, but implementation dependent. standard states that sizeof(int) >= sizeof(short), and short is at least 16 bits. so, for different compilers, sizeof(int) can be different even on the same OS.

            P D 2 Replies Last reply
            0
            • Z Zdeslav Vojkovic

              actually, it is not OS dependent, but implementation dependent. standard states that sizeof(int) >= sizeof(short), and short is at least 16 bits. so, for different compilers, sizeof(int) can be different even on the same OS.

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              and they are also processor dependent but not too much though. -Prakash -Prakash

              V 1 Reply Last reply
              0
              • P Prakash Nadar

                and they are also processor dependent but not too much though. -Prakash -Prakash

                V Offline
                V Offline
                vikas amin
                wrote on last edited by
                #7

                Processor dependent ????? :confused: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                P T 2 Replies Last reply
                0
                • V vikas amin

                  Processor dependent ????? :confused: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                  P Offline
                  P Offline
                  Prakash Nadar
                  wrote on last edited by
                  #8

                  Well to understand this u need to know how softwares are dependent on hardware (processor) generally the integer size is the size of word size of the processor, for 80386 onwards the word size is 32 bit and hence the integer size in most of the OS on Intel machine is 32bit. Why the size of integer varies from compiler to compiler/OS 2 OS, because integer is the most used datatype and transfering a 32bit of data from the memory to processor is more efficient than other sized data. -Prakash -Prakash

                  1 Reply Last reply
                  0
                  • S sunit5

                    size of int is OS dependent never say die

                    V Offline
                    V Offline
                    vikas amin
                    wrote on last edited by
                    #9

                    Probably i would preffer to say implementation dependent then processor dependent . :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                    P 1 Reply Last reply
                    0
                    • V vikas amin

                      Processor dependent ????? :confused: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                      T Offline
                      T Offline
                      ThatsAlok
                      wrote on last edited by
                      #10

                      vikas amin wrote:

                      Processor dependent ?????

                      In continuation with Mr. Prakash.. if you see := sizeof(long)==sizeof(int) in windows enviornment

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV

                      1 Reply Last reply
                      0
                      • V vikas amin

                        Probably i would preffer to say implementation dependent then processor dependent . :cool: Vikas Amin Embin Technology Bombay vikas.amin@embin.com

                        P Offline
                        P Offline
                        Prakash Nadar
                        wrote on last edited by
                        #11

                        vikas amin wrote:

                        Probably i would preffer to say implementation dependent then processor dependent .

                        yes but the implementation is dependent on the processor. :)


                        -Prakash

                        S 1 Reply Last reply
                        0
                        • Z Zdeslav Vojkovic

                          actually, it is not OS dependent, but implementation dependent. standard states that sizeof(int) >= sizeof(short), and short is at least 16 bits. so, for different compilers, sizeof(int) can be different even on the same OS.

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Zdeslav Vojkovic wrote:

                          ...sizeof(int) can be different even on the same OS.

                          Do you have a valid example of such? I've never seen an int that varied in size on the same machine.


                          "Take only what you need and leave the land as you found it." - Native American Proverb

                          G 1 Reply Last reply
                          0
                          • D David Crow

                            Zdeslav Vojkovic wrote:

                            ...sizeof(int) can be different even on the same OS.

                            Do you have a valid example of such? I've never seen an int that varied in size on the same machine.


                            "Take only what you need and leave the land as you found it." - Native American Proverb

                            G Offline
                            G Offline
                            Gary R Wheeler
                            wrote on last edited by
                            #13

                            This goes back a bit in time, but... In Microsoft C 6.0 (MS-DOS era), an int was 16 bits. Watcom C 9.0, on the same machine, made an int to be 32 bits. The Microsoft target in this case was 16-bit real-mode MS-DOS while the Watcom target was 32-bit protected mode MS-DOS.


                            Software Zen: delete this; // [Fold With Us!](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx)[[^](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx "New Window")]

                            D 1 Reply Last reply
                            0
                            • G Gary R Wheeler

                              This goes back a bit in time, but... In Microsoft C 6.0 (MS-DOS era), an int was 16 bits. Watcom C 9.0, on the same machine, made an int to be 32 bits. The Microsoft target in this case was 16-bit real-mode MS-DOS while the Watcom target was 32-bit protected mode MS-DOS.


                              Software Zen: delete this; // [Fold With Us!](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx)[[^](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx "New Window")]

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #14

                              But was it a true 32-bit int? That's like saying we have a 64-bit int today, but only because Microsoft's compiler mashes two 32-bit ints together, simulating a bigger one.


                              "Take only what you need and leave the land as you found it." - Native American Proverb

                              G 1 Reply Last reply
                              0
                              • D David Crow

                                But was it a true 32-bit int? That's like saying we have a 64-bit int today, but only because Microsoft's compiler mashes two 32-bit ints together, simulating a bigger one.


                                "Take only what you need and leave the land as you found it." - Native American Proverb

                                G Offline
                                G Offline
                                Gary R Wheeler
                                wrote on last edited by
                                #15

                                Actually it was. The 80386 processors and their descendents included direct 32-bit arithmetic and pointer support. The Watcom compiler supported protected mode execution with a flat memory model for a variety of DOS extenders. A "DOS extender" basically provided an execution environment for a protected mode application in the MS-DOS environment.

                                DavidCrow wrote:

                                Microsoft's compiler mashes two 32-bit ints together

                                Hmm. Is that still the case? I was under the impression that __int64 support since VS.NET 2002 was provided directly by native instructions.


                                Software Zen: delete this; // [Fold With Us!](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx)[[^](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx "New Window")]

                                D 1 Reply Last reply
                                0
                                • G Gary R Wheeler

                                  Actually it was. The 80386 processors and their descendents included direct 32-bit arithmetic and pointer support. The Watcom compiler supported protected mode execution with a flat memory model for a variety of DOS extenders. A "DOS extender" basically provided an execution environment for a protected mode application in the MS-DOS environment.

                                  DavidCrow wrote:

                                  Microsoft's compiler mashes two 32-bit ints together

                                  Hmm. Is that still the case? I was under the impression that __int64 support since VS.NET 2002 was provided directly by native instructions.


                                  Software Zen: delete this; // [Fold With Us!](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx)[[^](http://www.codeproject.com/script/profile/whos_who.asp?msg=1307432&id=10338#xx1307432xx "New Window")]

                                  D Offline
                                  D Offline
                                  David Crow
                                  wrote on last edited by
                                  #16

                                  Gary R. Wheeler wrote:

                                  Is that still the case?

                                  I'm not sure since I only use VC++ v6.

                                  Gary R. Wheeler wrote:

                                  I was under the impression that __int64 support since VS.NET 2002 was provided directly by native instructions.

                                  Even on processors that do not a have 64-bit bus?


                                  "Take only what you need and leave the land as you found it." - Native American Proverb

                                  1 Reply Last reply
                                  0
                                  • P Prakash Nadar

                                    vikas amin wrote:

                                    Probably i would preffer to say implementation dependent then processor dependent .

                                    yes but the implementation is dependent on the processor. :)


                                    -Prakash

                                    S Offline
                                    S Offline
                                    segment_fault
                                    wrote on last edited by
                                    #17

                                    The most important thing to do is to always use the "sizeof" operation in any code you're referring to. Never, ever hardcode the size into your code. When reading/writing to a file - there are exceptions -- in general, avoid using "int" as a type when it comes to storage of data structures. Instead, define types such as DWORD, WORD, BYTE, etc. This will save you a lot of trouble down the road. Read more of what I have to say at http://directx9.blogspot.com/

                                    P 1 Reply Last reply
                                    0
                                    • S segment_fault

                                      The most important thing to do is to always use the "sizeof" operation in any code you're referring to. Never, ever hardcode the size into your code. When reading/writing to a file - there are exceptions -- in general, avoid using "int" as a type when it comes to storage of data structures. Instead, define types such as DWORD, WORD, BYTE, etc. This will save you a lot of trouble down the road. Read more of what I have to say at http://directx9.blogspot.com/

                                      P Offline
                                      P Offline
                                      Prakash Nadar
                                      wrote on last edited by
                                      #18

                                      segment_fault wrote:

                                      The most important thing to do is to always use the "sizeof" operation in any code you're referring to. Never, ever hardcode the size into your code. When reading/writing to a file - there are exceptions -- in general, avoid using "int" as a type when it comes to storage of data structures. Instead, define types such as DWORD, WORD, BYTE, etc. This will save you a lot of trouble down the road.

                                      yes i know that.


                                      -Prakash

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups