Thread: unique data types

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    162

    unique data types

    i'm in the need of a 1 byte data type.
    i don't want to use 'char'.

    heck, theoretically, could i make a 4bit type? ... 2bit?

    what are my options?
    Last edited by simpleid; 11-04-2007 at 08:19 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Any class or struct you write creates a new datatype.

    All new datatypes will be at least one byte.

    Why don't you want to use char? If you want to keep your type separate from char you can wrap a class around a char. You can also use a typedef so your "type" has a different name, although in reality it will be the same type.

    If you want to save space and only use 4 bits, you can't create a datatype to do it, but you might be able to use other storage options to get a similar effect.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use bit-fields, but those are nasty.

    Just use unsigned char.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    i don't want to do this, to store the value;

    (unsigned char)( (short int) val )

    i don't want to cast either, to just store a value in to an unnecessarily large data type.
    on principle i'd like to make this as perfect as possible.
    so if anyone else knows, tell me.

    i'm going to look in to bit fields.


    the code i'm writing requires three large arrays, when i initialize them with INT the app crashes. if i use the smallest necessary types it works fine.

    at least until i scale the window large enough then it crashes anyway.

    i have 2gb of memory and the usage never goes over 1gb, any explanations for that? i might be able to get around having to do this if i can figure that out.
    Last edited by simpleid; 11-04-2007 at 08:40 PM.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    or just char, since char is unsigned by default (whereas short, long, int, long long int/"the equivalent in microsoft world" are signed by default)

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    the code i'm writing requires three large arrays, when i initialize them with INT the app crashes. if i use the smallest necessary types it works fine.

    at least until i scale the window large enough then it crashes anyway.
    that suggests poor design.

    Can you post a bit more code so we know what are you trying to do?

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Quote Originally Posted by cyberfish View Post
    that suggests poor design.

    Can you post a bit more code so we know what are you trying to do?

    alright, sure;

    this is global;
    Code:
    const int
    	AW = 512,
    	AH = 384;
    
    typedef struct RGB
    {
    	unsigned char r, g, b;
    };
    
    RGB Image[AW][AH];
    this is in the render loop, but *only* executes if the window is scaled;
    Code:
    CW = glutGet(GLUT_WINDOW_WIDTH);
    CH = glutGet(GLUT_WINDOW_HEIGHT);
    
    RGB AImage[CW][AH];
    RGB SImage[CW][CH];

    if the RGB struct uses SHORT INT, merely initiating SImage crashes the app. (using unsigned char)

    the app crashes anyway if the window is scaled beyond 800x600-ish.
    Last edited by simpleid; 11-04-2007 at 09:07 PM.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char is unsigned by default
    This isn't guaranteed. It depends on the implementation.

    What range of values are possible for your numbers?

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    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.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't see how large data types can crash your app.
    Even your "huge" array of RGB (even if you switch them out with ints instead of chars), would take about 2 MB of memory. Something else may be amiss.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    800x600 * 4 bytes is still only about 1.92 MB, there should be plenty of heap space for that. It might cause you to run out of stack space. Perhaps you should use dynamic memory like a vector to hold the data.

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Quote Originally Posted by Elysia View Post
    I don't see how large data types can crash your app.
    Even your "huge" array of RGB (even if you switch them out with ints instead of chars), would take about 2 MB of memory. Something else may be amiss.
    i've cut the application down to only the array initializations, and it crashes still in the same fashion i've described above.

  13. #13
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Quote Originally Posted by Daved View Post
    800x600 * 4 bytes is still only about 1.92 MB, there should be plenty of heap space for that. It might cause you to run out of stack space. Perhaps you should use dynamic memory like a vector to hold the data.
    hmm, you're right, i should use vectors since i'm writing c++.
    i'll swap it up.
    Last edited by simpleid; 11-04-2007 at 09:33 PM.

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    never mind! i forgot to change a couple things. the app doesn't crash anymore.

    thanks for the heads up on the vectors daved.
    and i guess i'll stick with unsigned chars.
    Last edited by simpleid; 11-04-2007 at 09:37 PM.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Edit: Apparently the problem was solved by the removal of the stack based arrays.
    Last edited by Daved; 11-05-2007 at 02:45 AM.

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