Friday, July 11, 2008
Alignment Matters
When do these two snippets behave differently?
Answer: when 'something' returns a value that's not a multiple of "sizeof(int)".
Some processor architectures only allow loading memory into N-byte registers from memory addresses that are a multiple of N (e.g. multiple of 4 for 32-bit registers). Using misaligned addresses can have some various interesting consequences, depending on the CPU; I've heard of at least these:
Last week, I got lucky: I discovered that the MIPS32 CPU on the phone I was programming falls in the "crash" category. (why lucky? because an "address load exception" message is much easier to debug than some corrupted data)
Usually, we don't have to worry about such alignment issues; the compiler and runtime make sure that all objects it allocates go at addresses that have the right alignment for their type (e.g. malloc must return memory "suitably aligned" for all possible types).
Trouble comes when we lie to the compiler, such as telling it by a cast that "p" points to an "int" when such is not the case. This is what happened to me: I was parsing a file, and the 4-byte-value-that-should-be-put-in-an-int followed an arbitrary-length string. It ended up on an odd address, and boom. (Here's another way I was lucky: it COULD have been a nice multiple of 4 in all my tests, only to come out odd on a client's desk)
Functions like memcpy, of course, are required to work with all addresses (as expressed by taking void* parameters, which require no cast).
Lesson of the day? Don't lie to your compiler!
(alternate lesson: "casts: evil AND chaotic"?)
void* p = something();
int i = *(int*)p;
int i;
void* p = something();
memcpy(&i, p, sizeof(int));
Answer: when 'something' returns a value that's not a multiple of "sizeof(int)".
Some processor architectures only allow loading memory into N-byte registers from memory addresses that are a multiple of N (e.g. multiple of 4 for 32-bit registers). Using misaligned addresses can have some various interesting consequences, depending on the CPU; I've heard of at least these:
- It works fine
- It works, but slowly (e.g. because the misalignment is detected and alternate instructions are used to load 1 byte at a time - I've heard that x86 works this way)
- The program may crash due on an "invalid address" trap
- The CPU may load data from the wrong address (e.g. if only multiples of 4 are valid addresses, the instruction might ignore the bottom 2 bits of the address)
Last week, I got lucky: I discovered that the MIPS32 CPU on the phone I was programming falls in the "crash" category. (why lucky? because an "address load exception" message is much easier to debug than some corrupted data)
Usually, we don't have to worry about such alignment issues; the compiler and runtime make sure that all objects it allocates go at addresses that have the right alignment for their type (e.g. malloc must return memory "suitably aligned" for all possible types).
Trouble comes when we lie to the compiler, such as telling it by a cast that "p" points to an "int" when such is not the case. This is what happened to me: I was parsing a file, and the 4-byte-value-that-should-be-put-in-an-int followed an arbitrary-length string. It ended up on an odd address, and boom. (Here's another way I was lucky: it COULD have been a nice multiple of 4 in all my tests, only to come out odd on a client's desk)
Functions like memcpy, of course, are required to work with all addresses (as expressed by taking void* parameters, which require no cast).
Lesson of the day? Don't lie to your compiler!
(alternate lesson: "casts: evil AND chaotic"?)
Labels: c++, compiler, optimization
Comments:
<< Home
Something tells me that from this page you will get some good ideas on how to use research tools. Make sure to take a look at it as soon as possible
Amazing blog! I have go through all the content of this blog.This is really helpful and informative for me.If you are looking to buy an online Dissertation Data Analysis Help service turn out to dissertation writing help company.
Great article! Alignment Matters alot, it does a fantastic job of outlining the key factors to consider when selecting a mobile app agency. It's an extremely helpful resource for anyone looking to hire a mobile app agency. The tips on understanding the process and expectations are especially valuable. Thanks for sharing this!
I just finished watching Macadamian Files and I absolutely loved it! It was such a great show and I definitely recommend it to any fans of mystery and detective drama. The story was exciting and the actors did a great job bringing the characters to life. I especially loved the twist at the end! If you haven't seen it yet, you should definitely check it out.
Thomas, a dedicated Do My Dissertation, collaborates with Dissertation Educators to help students overcome writing challenges. His expertise and support make the process smooth and efficient.
Think about retaining the services of S&R Law Firm, a reputable traffic law firm with a track record of success in reckless driving defense, if you need representation in Fairfax, VA for accusations related to reckless driving.
reckless driving virginia fairfax
Post a Comment
reckless driving virginia fairfax
<< Home