Thread: sizeof

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    57

    Question sizeof

    Hi,
    My question is
    at what level is "sizeof" operator resolved?

    to elaborate,

    if I have sizeof(int), what decides the size of int? the CPU? if yes then would the sizeof(int) give the same value when executed in the same machine say once in linux and then in windows in a dual boot system?

    if compiler decides it then can it be played around? (Just a figment of my imagination.)

    thanks,
    Anoop.

  2. #2
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    It's definitely determined by the hardware thats for sure, and i also believed ( not 100% ) that it's the CPU.

    Not sure for your 2nd question.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    The Instruction Set Architecture (ISA) decides it. These are the instructions that the CPU understands and are specific to the CPU.

    Now it is possible to define diferent sizes, but then you also have to define the operations. An example of this was before a float was standardized, different implementations existed for it. These had to be defined, usually in assembly.

    If you look in limits.h file, you should see a lot if conditional logic for defining the sizes of primatives based on the processor and/or ISA.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    57

    sizeof

    Does that mean that every time the compiler encounters the sizeof operator, it has to switch to the kernel mode and interact with the machine. That would be too costly.
    And since if limits.h defines the limit then that means we can manipulate it and make a program crash during runtime.

    I could be terribly wrong in my assumptions so please excuse them. Its just that it has baffled me.
    Thanks
    Anoop.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Location
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    Posts
    61
    When ever the sizeof operator is encountered it refers to the value defined in limits.h.

    Now if you changed limits.h it could cause compile and runtime errors with the application, but would not affect the operating system. Basically, limits.h is one of those files that you should not edit.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    so i guess basically u could have different values in limits.h in different systems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Cutting up an std::string
    By Shamino in forum C++ Programming
    Replies: 26
    Last Post: 04-04-2006, 11:02 AM
  5. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM