J
J. Campbell
I posted a question some time back about accessing a char array as an
array of words. In order not to overrun the char array, I padded it
with enough 0x00 bytes to ensure that when accessed as words I
wouldn't overrun the array. I was told that this is dangerous and
that there could be alignment problems if, for example, I wanted to
access the char array elements from non-even multiples of sizeof(int).
For example, if I had the array:
char a[10];
and I wanted to access the 8 bytes (a[2], a[3],..., a[8], a[9]) as the
array:
int b[2];
where (b[0] contains the data in a[2] to a[5], and b[1] contains a[6]
to a[9])
I understand the alignment issue in this example. My question
is...can I turn this problem on its head...for example, create an
empty array of ints, then access this memory space as a char?
Here's what I'm talking about:
unsigned int* a_words;
char* a_bytes;
fstream in("myfile.dat", ios::in | ios::binary | ios::ate);
int filesize_bytes = in.tellg();
int filesize_words = filesize_bytes / sizeof(int) + ((filesize_bytes %
sizeof(int)) > 0); // add 1 if there is a remander...
a_words = new unsigned int[filesize_words];
a_bytes = reinterpret_cast<char*>(a_words);
in.seekg(2, ios::beg); //note...out of (word) alignment...starts on
3rd byte
in.read(a_bytes, filesize_bytes-3);
in.close();
at which point the file is in memory and can be accessed as bytes (by
indexing a_bytes[0 to filesize_bytes]) or as words (by indexing
a_words[0 to filesize_words].
This seems to work fine. Additionally, it shouldn't suffer potential
alignment problems since the array is defined to align with words, and
word addresses should be accessable to a byte address, even if the
converse of this is not true.
I can see that there will be compatibility problems with this system
if ported to a system where CHAR_BIT != 8. However, I don't care
about these systems. If I'm only doing logical operators on the bits
in the file, I don't even see any endian issues with doing this.
Thanks for the slap-in-the-face I'm sure I'll get for performing such
blastphomous operations in c++. Seriously, does this treatment
circumvent potential alignment issues?
array of words. In order not to overrun the char array, I padded it
with enough 0x00 bytes to ensure that when accessed as words I
wouldn't overrun the array. I was told that this is dangerous and
that there could be alignment problems if, for example, I wanted to
access the char array elements from non-even multiples of sizeof(int).
For example, if I had the array:
char a[10];
and I wanted to access the 8 bytes (a[2], a[3],..., a[8], a[9]) as the
array:
int b[2];
where (b[0] contains the data in a[2] to a[5], and b[1] contains a[6]
to a[9])
I understand the alignment issue in this example. My question
is...can I turn this problem on its head...for example, create an
empty array of ints, then access this memory space as a char?
Here's what I'm talking about:
unsigned int* a_words;
char* a_bytes;
fstream in("myfile.dat", ios::in | ios::binary | ios::ate);
int filesize_bytes = in.tellg();
int filesize_words = filesize_bytes / sizeof(int) + ((filesize_bytes %
sizeof(int)) > 0); // add 1 if there is a remander...
a_words = new unsigned int[filesize_words];
a_bytes = reinterpret_cast<char*>(a_words);
in.seekg(2, ios::beg); //note...out of (word) alignment...starts on
3rd byte
in.read(a_bytes, filesize_bytes-3);
in.close();
at which point the file is in memory and can be accessed as bytes (by
indexing a_bytes[0 to filesize_bytes]) or as words (by indexing
a_words[0 to filesize_words].
This seems to work fine. Additionally, it shouldn't suffer potential
alignment problems since the array is defined to align with words, and
word addresses should be accessable to a byte address, even if the
converse of this is not true.
I can see that there will be compatibility problems with this system
if ported to a system where CHAR_BIT != 8. However, I don't care
about these systems. If I'm only doing logical operators on the bits
in the file, I don't even see any endian issues with doing this.
Thanks for the slap-in-the-face I'm sure I'll get for performing such
blastphomous operations in c++. Seriously, does this treatment
circumvent potential alignment issues?