1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Vary size of char from 1 to 2

Discussion in 'Embedded' started by RahulS, Jan 10, 2006.

  1. RahulS

    RahulS Guest

    Hi,
    I am using ARM Developer Suite by Codewarrior on Hynix processor with
    an ARM7 core.
    I want to configure size of few datatypes.
    e.g.: I want to configure the size of char as 2 or 4 accordingly.
    Is it possible using the complier features? I was unable to find
    anything in the complier menu.
    Kindly guide me through this.
    Regards
    RahulS
     
    RahulS, Jan 10, 2006
    #1
    1. Advertisements

  2. RahulS

    David Brown Guest

    What is it you are actually trying to achieve here?

    In C, sizeof(char) is always 1 (by definition), and it is always the
    smallest size the target can directly access. Since the ARM can
    directly access 8-bit bytes, that is the size of a char. The most that
    you can do with most compilers is chose whether the default is signed or
    unsigned, although if your code depends on that setting, your code is
    badly written.

    Trying to write C on architectures with chars greater than 8-bit is a
    PITA. It's not something one would chose voluntarily.
     
    David Brown, Jan 10, 2006
    #2
    1. Advertisements

  3. No way. If you're very lucky, you may manage to change the number of
    bytes occupied by a char, but you cannot ever possibly be allowed to
    change the size of it. If you managed to get sizeof(char)!=1, that
    beast you created would no longer be a C compiler.

    Anyway: what on earth do you hope to achieve by this?
     
    Hans-Bernhard Broeker, Jan 10, 2006
    #3
  4. Not always. It's the smallest size the target can address that
    is larger than 7 bits. Or something like that. On a machine
    that can directly address 1 bit, a char is not allowed to be 1
    bit.
    It's not that bad. You've got to pay attention to what you're
    doing, but no more so than is normally required in an usafe
    language like C.
    Writing C is not something one would choose voluntarily. ;)
     
    Grant Edwards, Jan 10, 2006
    #4
  5. Are you trying to use Unicode characters ?

    At least in C99 this might be possible using <wchar.h> if it contains
    sensible definitions.

    Paul
     
    Paul Keinanen, Jan 10, 2006
    #5
  6. RahulS

    Jack Klein Guest

    Not that bad, you just have to be careful.

    I've been doing it on a TI motor control DSP for quite some time now.
    Char, short, and int are all 16 bits, long is 32. Did some work in
    the past on an Analog Devices 32-bit DSP where char, short, int, and
    long were all 32 bits.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Jan 11, 2006
    #6
  7. RahulS

    RahulS Guest

    I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
    byte. When i try to write a lower byte(char) onto this by enabling the
    lower byte pin, it writes that to both lower as well as higher byte and
    vice-versa.
    When i try to write a single word, it writes it correctly on both the
    lower and upper bytes.
    Problem is only when i try writing a byte or in other words a ' char '.
    That is why, i was trying to change the size of char from a byte to 2
    bytes.

    Can you suggest me some other way to overcome this problem.
    Thanks
    RahulS
     
    RahulS, Jan 11, 2006
    #7
  8. RahulS

    RahulS Guest

    I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
    byte. When i try to write a lower byte(char) onto this by enabling the
    lower byte pin, it writes that to both lower as well as higher byte and
    vice-versa.
    When i try to write a single word, it writes it correctly on both the
    lower and upper bytes.
    Problem is only when i try writing a byte or in other words a ' char '.
    That is why, i was trying to change the size of char from a byte to 2
    bytes.

    Can you suggest me some other way to overcome this problem.
    Thanks
    RahulS
     
    RahulS, Jan 11, 2006
    #8
  9. RahulS

    RahulS Guest

    I am using M68AW512M RAM.It has 2 pins to read/write the upper or lower
    byte. When i try to write a lower byte(char) onto this by enabling the
    lower byte pin, it writes that to both lower as well as higher byte and
    vice-versa.
    When i try to write a single word, it writes it correctly on both the
    lower and upper bytes.
    Problem is only when i try writing a byte or in other words a ' char '.
    That is why, i was trying to change the size of char from a byte to 2
    bytes.

    Can you suggest me some other way to overcome this problem.
    Thanks
    RahulS
     
    RahulS, Jan 11, 2006
    #9
  10. RahulS

    David Brown Guest

    Yes, that's true - a char must be at least 8 bits. But as sizeof(char)
    is always 1, and all other types are defined as multiples of char, C
    itself is unable to directly address anything smaller even if the target
    architecture supports it. Unless, of course, your compiler has
    non-standard extensions...
    It's an extra thing to keep in mind. For many tasks, it makes little
    difference, but when trying to manipulate character-based telegrams
    using the least possible ram space it is very much in the way. It
    certainly leads to unportable code (you don't want code filled with byte
    extraction functions if you don't need it!).
     
    David Brown, Jan 11, 2006
    #10
  11. RahulS

    David Brown Guest

    If you *must* use google groups, please learn to use it properly so that
    context is included in your replies.

    As for your problem, it's a hardware issue, and depends greatly on the
    particular microcontroller connected to the RAM. Microcontrollers which
    directly access 16-bit (or greater) buses always have some sort of byte
    select or byte mask pins that are used for precisely this purpose - they
    are used to direct the ram to only store in the correct bytes.
     
    David Brown, Jan 11, 2006
    #11
  12. Your hardware is broken.
    I suggest you fix your hardware.
     
    Grant Edwards, Jan 11, 2006
    #12
  13. It would be easier if the pre-processor knew the sizes of
    types. That way the byte-extraction code could be
    automagically included or not. Since that's not the case,
    using macros that are conditional on the target architecture
    works pretty well.
     
    Grant Edwards, Jan 11, 2006
    #13
  14. Nonsense. Of course it knows! <limits.h> is guaranteed to be
    available in every C compiler rightfully bearing that name, and it
    contains all the information needed in a form the preprocessor can
    access. For starters, all you need is a single

    #include <limits.h>

    #if CHAR_BIT > 8
    # error Arghhh!
    #endif

    in the right place if the source.
     
    Hans-Bernhard Broeker, Jan 11, 2006
    #14
  15. You're right. I forgot about that. [However, this being.
    c.a.e, a lot of people are stuck using C compilers not
    rightfully bearing that name.]
     
    Grant Edwards, Jan 11, 2006
    #15
  16. RahulS

    David Brown Guest

    Yes, theoretically you can use this sort of pre-processor stuff to write
    "portable" code that takes into account the width of a char. But you
    cannot write good, clean, legible code (for things like character-based
    messages) that compiles efficiently and works well on 8-bit char and
    16-bit char (and 32-bit char) architectures. limits.h and
    pre-processing directives can give you efficient code, or legible code,
    but not both.

    Having said that, I've written programs on 16-bit char targets, and they
    work. It's just another inconvenience to deal with.
     
    David Brown, Jan 12, 2006
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.