469,338 Members | 8,644 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

memory alignment

Hi All,
Im having a program
int main()
{
char* p = "hello";
printf("%d",*((int*)p));

}

char* is acutally non aligned and we r casting it to int* and
accessing the value, will there be any perfomance issue related to
alignment.
Thanks
Aneesh
Nov 13 '05 #1
3 7409
aneesh <an********@hotmail.com> wrote:
Hi All,
Im having a program
int main()
{
char* p = "hello";
printf("%d",*((int*)p)); } char* is acutally non aligned and we r casting it to int* and
accessing the value, will there be any perfomance issue related to
alignment.


There may not only be performance issues but, even worse, on archi-
tectures that require proper alignment you may get a bus error and
your program will get aborted. But you should ask this in a newsgroup
dedicated to the architecture you're using, standard C can't give you
any final answers on this.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #2
"aneesh" <an********@hotmail.com> wrote:

You need:
#include <stdio.h>
int main()
{
char* p = "hello";
printf("%d",*((int*)p));
Add:
return 0;
}

char* is acutally non aligned and we r casting it to int* and
accessing the value, will there be any perfomance issue related to
alignment.


Performance issues are the least of your worries. This is undefined
behaviour for good reasons.

a) If sizeof(int) > 6 then you are accessing out of the bounds of
the array.
b) If the object when interpreted as an integer forms a trap
representation your program may crash.
c) If the alignment is wrong for an integer, your program may crash
with a 'bus error' or equivalent.

--
Simon.
Nov 13 '05 #3
an********@hotmail.com (aneesh) writes:
Hi All,
Im having a program
int main()
{
char* p = "hello";
printf("%d",*((int*)p));

}

char* is acutally non aligned and we r casting it to int* and
accessing the value, will there be any perfomance issue related to
alignment.


Alignment problems lead to undefined behavior, which might be
simply lessened performance, or might be system-wide crash, or
might be to invoke nasal daemons. Aside from alignment, the mere
accessing of p through an lvalue of incompatible type, and the
lack of declaration for printf(), also invoke undefined behavior.

-Micah
Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Tron Thomas | last post: by
7 posts views Thread by serikas | last post: by
13 posts views Thread by Kutty Banerjee | last post: by
13 posts views Thread by sachin_mzn | last post: by
7 posts views Thread by Dhirendra Pal Singh | last post: by
11 posts views Thread by simonp | last post: by
29 posts views Thread by K. Jennings | last post: by
13 posts views Thread by Chris Thomasson | last post: by
2 posts views Thread by somenath | last post: by
8 posts views Thread by ramsatishv | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.