Thread: unique data types

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by simpleid View Post
    char is not unsigned by default on my machine.
    i need to store values no larger than 255 for each of the three primary color components.

    storing the three color components in to an int was unnecessary since it offered too much space.
    just fyi in case anyone mentions it. and as i've said, too large of data types crashed my app.
    If you need values up to 255, you can't use anything smaller than an unsigned char.

    Also, you might want to do something like this:
    Code:
    typedef unsigned char   color;
    That way, in the future if you decide you need more than 255 shades/color, it will be much easier to change. Plus it would make the meaning of your code clearer.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Don't create things that are larger than a few kB on the stack. You only have about 1 MB of stack space on most systems. Allocate such things from the heap.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by simpleid View Post
    theoretically, could i make a 4bit type? ... 2bit?
    You could do something along the lines of the vector<bool> specialisation to achieve such a thing. At last that's where I'd start if I were going to make that.
    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"

  4. #19
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    theoretically, could i make a 4bit type? ... 2bit?
    i would make a class that emulates a "collection" of such type, and use bitwise operations to implement operations on them. Note that it will be very inefficient though. For this particular case, just allocate the memory on the heap (new or malloc).

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    vector<bool> is evil, and very likely to be deprecated in the next standard.

    Use std::bitset if you have a collection of known size, or boost::dynamic_bitset if it's of unknown size. But in this case, I agree with cyberfish.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I worked on a C++ project that simulated a complete chip, and it had, as part of the design, n-bit wide types from 1 to 512 bits. Although, as a space-saving excercise, it wouldn't be particularly useful, since each integer was a class, which takes up more than one byte [because to make it efficient, the class essentially had two fields, one to hold the value and one that was the mask whcih whcih to extract the actual value after any math operation].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 10:43 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM