473,785 Members | 2,327 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questions about "alignment" in memory

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_byte s %
sizeof(int)) > 0); // add 1 if there is a remander...

a_words = new unsigned int[filesize_words];
a_bytes = reinterpret_cas t<char*>(a_word s);

in.seekg(2, ios::beg); //note...out of (word) alignment...sta rts 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?
Jul 19 '05 #1
14 4096
WW
J. Campbell wrote:
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?


Yes you can, but only with char being the "other" thing. Another solution
is to define a union, with a char and an int array inside.

--
WW aka Attila
Jul 19 '05 #2
WW wrote:
Yes you can, but only with char being the "other" thing. Another solution
is to define a union, with a char and an int array inside.

This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.

Many implementations do allow it.

There are more portable ways, basically shifting and or-ing the bytes
onto an int.


Brian Rodenborn
Jul 19 '05 #3
WW
Default User wrote:
WW wrote:
Yes you can, but only with char being the "other" thing. Another
solution is to define a union, with a char and an int array inside.

This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.


yep. But we are talking about a char and an int array so far.

--
WW aka Attila
Jul 19 '05 #4
WW wrote:

Default User wrote:
WW wrote:
Yes you can, but only with char being the "other" thing. Another
solution is to define a union, with a char and an int array inside.

This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.


yep. But we are talking about a char and an int array so far.

Right, which don't come under the exemption. If I got the OP's problem
right, he had a buffer of char that he wanted to convert into a series
of ints. Using unions to do so would be implementation-defined behavior
(if I'm reading the standard correctly).

Here's a way from my personal library:

unsigned int CreateDataWord (unsigned char data[4])
{
unsigned int dataword = 0;

for (int i = 0; i < 4; i++)
{
dataword |= data[i] << (3-i) * 8;
}
return dataword;
}
Note that this uses unsigned char for the buffer, which is guaranteed to
be safe, requires CHAR_BIT == 8, and is predicated on 32-bit int, so it
has its own nonportabilitie s.

Brian Rodenborn
Jul 19 '05 #5
"WW" <wo***@freemail .hu> wrote in:
yep. But we are talking about a char and an int array so far.


Thanks...<so far :-)>

Indeed, the real question is: is it SAFE to access a region of
memory, defined as other than char, as a char array...if you are aware
of the issues? Your answer indicates a cautious "yes" if you are
gentle, and make sure never to overstep the char array bounds...as
long as CHAR_BIT is the length expected. Is this interpretation
correct??

Thanks for the response...stil l trying to learn...6 mos into the
process...still love QB45...;-)
Jul 19 '05 #6
Default User <fi********@boe ing.com.invalid > wrote in message news:<3F******* ********@boeing .com.invalid>.. .
WW wrote:
Yes you can, but only with char being the "other" thing. Another solution
is to define a union, with a char and an int array inside.

This is not guaranteed. It is implementation-defined behavior if the
value of a member of a union object is used when the most recent store
to the object was to a different member, other than structs sharing a
common initial sequence.

Many implementations do allow it.

There are more portable ways, basically shifting and or-ing the bytes
onto an int.

Brian Rodenborn


Brian,

So...you raise issue with the use of union...but what about my
original solution where I take a char array and put it into an int
array...which I then access as both an int and a char array. Are
there alignment problems with this, or are the problems more local???

I somehow get the feeling you are posting from Galviston...if this is
the case, then it explains the dissarray. Cheers, ciao, and thanks in
advance for the c++ help.
Jul 19 '05 #7
"J. Campbell" <ma**********@y ahoo.com> wrote in message
news:b9******** *************** ***@posting.goo gle.com...
[...]
So...you raise issue with the use of union...but what about my
original solution where I take a char array and put it into an int
array...which I then access as both an int and a char array. Are
there alignment problems with this, or are the problems more
local???
[...]


You would need to do a reinterpret cast, and that is not one of
the portable types for it. So technically, no. Doing what you
suggest will result in an ill-formed program (or maybe the
behaviour is just implementation-defined). On the other hand,
it will probably work on 99% of the compilers and systems out
there. Since it would be costly to do it the "right" way, I
personally would just run with it. But that's just me, and this is
a C++ newsgroup, so if I were toeing the party line like a good
programmer, I would revile you for suggesting a program which
might possibly contravene the sacred text which is the C++
standard. Anyway, good luck.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #8
"J. Campbell" wrote:
So...you raise issue with the use of union...but what about my
original solution where I take a char array and put it into an int
array...which I then access as both an int and a char array. Are
there alignment problems with this, or are the problems more local???

You can access any object as an array of unsigned char safely. That's
because unsigned char is guaranteed to have no trap representations . An
array of ints can be accessed as unsigned char. However, you then must
be cognizant of endianess of the ints in the array. It's generally kind
of tricky, I've found it easier and more portable (no method is
completely portable) to use bitwise operators.


Brian Rodenborn
Jul 19 '05 #9
"David B. Held" <dh***@codelogi cconsulting.com > wrote in message
news:bm******** **@news.astound .net...
[...]
On the other hand, it will probably work on 99% of the
compilers and systems out there.
[...]


After reading Default User's post, I realized I should have added
the caveat that it will probably work on 99% of the compilers
and systems out there *but in a generally non-portable way*.
That means that since you're reading raw bytes into an array
from a file, and assuming a certain byte order for int, the code
obviously won't work on a platform that has a different byte order.
But usually, people who do stuff like this aren't interested in
portability in the first place.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
20861
by: signuts | last post by:
I am wondering what it means when a pointer is aligned? Could someone perhaps enlighten me or point me in the right direction? Thank you in advance. -- Sig
11
8907
by: L. Chen | last post by:
The standard says that a char* or void* pointer has the least strict alignment. But I do not know what is a strict alignment. What does that mean?
12
8129
by: Olaf Baeyens | last post by:
I am porting some of my buffer class code for C++ to C#. This C++ class allocates a block of memory using m_pBuffer=new BYTE; But since the class is also used for pointers for funtions that uses raw MMX and SSE power, the starting pointer MUST be starting at a 16 byte memory boundary. In C++ I allocate more memory than needed, and in a second phase I search for the address that starts on a 16 byte boundary. And I then use that new...
5
3914
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are with the same solution. All had "Default" set in the "Struct Member Alignment" entry. After some assembler debugging we found out that a struct member that is a member function pointer in
9
3054
by: Oliver Block | last post by:
Hi, what is the most elegent way to center an image inside a web page. The image is radomly chosen by a cgi script may be 300x400 or 400x300. Are there any alignment commands for images?
0
1251
by: Jean-François Michaud | last post by:
Hello, I was wondering if there was a way around leader-alignment. XSF V3.4 from Antenna House seems to be a very powerful FO -> PDF converter, but it doesn't support this particular attribute from fo:leader. It doesn't support leader-pattern-width either but this can be bypassed by using leader-pattern="use-content" as such: <fo:leader leader-pattern="use-content"> . </fo:leader>
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.