473,385 Members | 1,893 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,385 software developers and data experts.

What is "Pointer Alignment"

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
Thu Sep 18 12:21:07 EDT 2003

Nov 13 '05 #1
3 20685
signuts wrote:
I am wondering what it means when a pointer is aligned?


On some architectures, some data types can or should only be accessed
if their address is a multiple of 4 or the size of the datatype or
whatever. Otherwise the program crashes, or gives wrong results, or
maybe the pointer access just goes a lot slower.

For a type T, a pointer is aligned for T access if it contains an
address which is a multiple of whatever number T* pointers should be a
multiple of.

--
Hallvard
Nov 13 '05 #2
On Thu, 18 Sep 2003 12:22:33 -0400, signuts <ex****@triton.net> wrote:
I am wondering what it means when a pointer is aligned?

Could someone perhaps enlighten me or point me in the right direction?


In some systems, certain data types can only be stored such their addresses are
all on an even number boundary. In other systems, some data types require
addresses be on 4 octet boundaries.

If you had

char c;
char *Pc = &c;
int *Pi;

and you

Pi = (int *)Pc;

you might find that references to *Pi would cause errors because Pc didn't point
to a data item aligned on an "integer" boundary.

--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 13 '05 #3
signuts <ex****@triton.net> wrote:
I am wondering what it means when a pointer is aligned?
Could someone perhaps enlighten me or point me in the right direction?


Since this isn't directly C related off-topicality warning:

<OT>
There are several architectures where e.g. an integer must start at
an even address (or addresses that can be divided by 4). This is
related to the machines commands for e.g. loading an int from memory
into the CPU: when an int gets loaded it's probably done using a
machine instruction that roughly translates to "load word from
memory address 0xc73204". If you try to load a "word" from an odd
address this machine instruction does not work and you get a bus
error.

But in C you can write code that looks like this (error checking
omitted):

int a;
unsigned char *buf = malloc( 128 );
a = * ( int * ) ( buf + 1 );

malloc() will always return memory that's properly aligned, i.e.
the address you get can be used for all kinds of types without
running into danger of getting a bus error. So buf will be an
even address. But when you now try to assign to 'a' in the way
it's done above, you try to load 'a' from an odd memory address
and then you're in trouble, at least on machines which require
proper alignment (on other machines this might only result in a
slower memory access). So the above (if it's really necessary)
must be written e.g. as

memcpy( &a, buf + 1, sizeof a );

because memcpy() has to be written in a way that won't use a
"load word" (or similar) instruction that could result in a
bus error.

BTW, this is also the reason why structures often need padding.
If you have a structure like

struct {
char c;
int a;
} my_struct;

the compiler will have to insert at least one extra byte between
'c' and 'a' because otherwise 'a' would end up at an odd address
and

my_struct.a = 0;

might lead to a bus error.
</OT>
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4

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

Similar topics

14
by: J. Campbell | last post by:
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...
12
by: Markus.Elfring | last post by:
I am interested to know if the pointer value for the memory address can be changed by a compiler if the constness of the corresponding type is cast away. inline char* deconstify1(char const * X)...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
11
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?
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.