views:

148

answers:

2

What Is the difference between how the char datatype is stored or represented in 32bit vs 64bit in C?

+8  A: 

There is no difference.
One char occupies one byte.
One byte has CHAR_BIT bits.

#include <limits.h>
#include <stdio.h>

int main(void) {
    printf("a char occupies 1 byte of %d bits.\n", CHAR_BIT);
    return 0;
}
pmg
thanks! just wanted to check to make sure.
pxl
I thought char was guaranteed to be at least 8 bits wide. A compiler can choose to use more than 8 bits if it wants. In theory a 64 bit x86 compiler could chose a different representation to a 32 bit x86 compiler.
Andrew Bainbridge
i've read somewhere that while this was the intent, to keep the same representation. but the way its stored may cause problems when the same code is compiled in 64 or 32bit. that is the problem i'm having now, how do i make sure i'm handling the sizing correctly (for, say, malloc(sizeof(char*)*5)) in the general case to always work (i.e. future proof it for 128bit and beyond).
pxl
@pxl `sizeof(char)` is always 1. `sizeof(char *)` is an entirely different beast.
Sinan Ünür
yes, i'm finding that out the hard way.
pxl
thanks everyone for the input. It's all become much clearer now.
pxl
Andrew Bainbridge - no, a char is pretty much always 8 bits. A pointer to a char could be 32 or 64 bits, and different char variables could be aligned to start on a 32,64 or 128 bit memory boundary.
Martin Beckett
in what instances would memory alignment cause a problem when migrating between 32bit and 64bit? I'm guessing for most, it won't matter unless you're messing around with memory manually.
pxl
+4  A: 

One possible difference is that chars might be aligned on 64bit rather than 32bit boundaries.

struct {
  char a;
  char b;
}

Might take up 2 * 4 bytes on 32bit and 2 * 8 bytes on 64bit.

edit -actually it wouldn't. Any sane complier would repack a struct with only chars on byte boundary. However if you added a 'long c;' in the end anything could happen. That's why a) you have sizeof() and b) you should be careful doing manual pointer stuff in c.

Martin Beckett
the "might" is what scares me. how would i make sure? and if i can't be sure, how would you work around it, to ensure that the same code compiles and works as expected for both 32bit and 64bit architectures?
pxl
Unless you are deliberately messing about with the memory it shouldn't matter. You can check the size of a struct at runtime but alignment is usually a compiler option
Martin Beckett
awesome, thanks for the follow up
pxl
Unlikely, since if it did then C-style strings would be huge. Arrays have to respect the same alignment requirements of the type as structs do. I'm not even sure this would be a valid implementation unless CHAR_BIT were 32 or 64 respectively. char is a special type and you're supposed to be able to use chars to access the bytes of any other object in memory. This would not be the case if char arrays had alignment padding.
Steve Jessop
There is no relation between how structs and strings are stored in memory.
Martin Beckett
No. Chars are never aligned, only bigger datatypes like short and int are aligned on 16 respectively 32 bit boundaries.
codymanix
But if a char follows a larger type it will be aligned on a different boundary depending on the CPU, OS and compiler
Martin Beckett