Thread: bitfield memory question

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Hunter2
    >>everything has to be at least a multiple of sizeof(char) in size.
    Right, but why is it forced to 32 bits (as opposed to 8), or is that a typo?
    [aside]
    I had a complier decide that when I said unsigned int for a bitfield that I meant it. And it chose that minimum size for the containing struct -- which was twice as big as I wanted. I had to use the nonstandard unsigned char to tell it to use a smaller type.
    [/aside]

    I don't think it is forced, I believe it is one of those implementor's descretion type of things. (The DWIM instruction.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but in a word, speed.
    Odd, I thought bitfields conceptually lend themselves better to the "trade speed for memory" end of the spectrum. If that's the case then, bitfields seem to be a waste of both speed and memory

    @Dave: Interesting read
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Right, but why is it forced to 32 bits (as opposed to 8), or is that a typo?
    Because that's what your compiler writer decided was the best thing to do.
    If you have a lot of bit fields, the best the compiler can do is pick the biggest supported underlying data type (say unsigned long), and pack as many bitfields as possible into that.

    Now if you want to try and reduce that, say to force type:5 and locale:1 into just one byte, then you might try your compiler specific
    #pragma pack(1)
    or in gcc, something like attribute((__packed__))

    > "trade speed for memory" end of the spectrum.
    Try some "gcc -S" experiments to see what sort of asm code is generated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Hunter2
    >>but in a word, speed.
    Odd, I thought bitfields conceptually lend themselves better to the "trade speed for memory" end of the spectrum. If that's the case then, bitfields seem to be a waste of both speed and memory

    @Dave: Interesting read
    Depends onthe compiler. Bitfields are implementation specific. I believe there's a post around here some place where a bunch of us did some bitfield stuff, and MSVC++ had a horrible implementation of them, which turned out far slower results than the rest of them.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>the best the compiler can do is pick the biggest supported underlying data type (say unsigned long), and pack as many bitfields as possible into that.
    Oh, I thought you meant that each bitfield was forced to 32 bits (i.e. the bitfield sizes are ignored, -> ). Makes more sense now.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 03:36 AM
  2. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. I have a Question about memory usage for C.
    By bobthefish3 in forum C Programming
    Replies: 34
    Last Post: 12-24-2001, 04:37 PM