473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 7690
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
7
by: serikas | last post by:
Is there a way to get aligned dynamically allocated memory? (provided that the requested memory size is a power of 2.) For example, if I request 128 bytes of memory, can I implement an allocator...
13
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long...
13
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the...
7
by: Dhirendra Pal Singh | last post by:
Hi all, I have couple of questions, I hope I can find the answers here...(assuming the question are okay with this group) A) what is memory alignment? I have a rough idea but cant clearly...
11
by: simonp | last post by:
I'm taking an intro course on C++, and our teacher is not being clear on how stuct memory padding is determined. If the memory used by all components defined inside a struct falls between a...
29
by: K. Jennings | last post by:
I would like to get the result of malloc() such that it is aligned on a given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will u64 *p = malloc(N) ; do the trick in general? ...
13
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
8
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc?...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.