Thread: A different sizeof()?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    A different sizeof()?

    I was wondering if there was a way to makea function, that like the sizeof() function returning the byte number, it returns the bit number?

    Code:
    //for example
    printf("%d bytes",sizeof(int));
    
    //prints
     4 bytes
    Code:
    // I want to make a function modification, or a new function that prints amount of bits
    printf("%d bits",sizeofbits(int));  //a new functions
    
    //prints
     32 bits

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So multliply by CHAR_BITS then.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    well, I want the function or function modification to be used in special cases when the variable may not be an exact bit number divisible by 8(# of bits in a byte).....
    Like say a bit field
    Code:
     struct bitfield{                        
                      unsigned first: 1;
                      unsigned second: 2;
                       }  struct1;   // this allocates a struct with 3 bits and no bytes
    int main()
    {
         printf("%d", sizeof(struct bitfield));
    }
    //Would print either a zero or a one depending on machine,which isn't exact
    I want a more exact as the amount of bits allocated in variable
    Code:
     struct bitfield{                        
                      unsigned first: 1;
                      unsigned second: 2;
                       }  struct1;   // this allocates a variable of a struct with 3 bits and no bytes
    int main()
    {
         printf("%d", sizeofbits(struct1));  // a new function,  or function modification
    }
    //prints exactly 3 bits

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your belief that your bitfield struct is only three bits long is just that, a belief. It is not necessarily in accordance with reality.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    So the struct is not exact......?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The size of structs in implementation-dependent. I, personally, would be very surprised to come across an implementation that allocated three bits for that struct. I would expect eight bits, with an outside chance of sixteen.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    So most likely computers always allocates to an sequence of 1 byte or more (which is machine dependent)...
    Even on a sega genesis(8-bit machine) I bet

    Thanks tabstop, that clears up alot for more, i appreciate it

    (I was always wondering why my computer always allocated to 4 bytes, but that was because my machine always located at least to 4 bytes because its 32 bit machine, duh! I feel retarded)

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It may also help to know that sizeof is not a function, it's a keyword. You don't need the brackets when the argument is a type for example, iirc. There's no run-time penalty at all as there would be if it were a function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,390
    Quote Originally Posted by iMalc
    You don't need the brackets when the argument is a type for example, iirc.
    Actually, you need the parentheses when the operand is a type name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ah well that shows how often I leave the brackets out! My point is that they can be left out in some cases anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

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. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 07:00 PM
  5. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 03:54 PM