468,752 Members | 1,312 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

how to get aliagned memory using malloc

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
understand it clearly...? If anyone has a pointer on net please tell
me.. I would be grateful...
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?
B) so how can I write a function for example...
void * align_malloc(size_t size_of_memory_required, int
bit_alignement_required)

Any help would be appreciated..

Thanks and advance

Dp
Nov 14 '05 #1
7 4822
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dhirendra Pal Singh wrote:
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
understand it clearly...? If anyone has a pointer on net please tell
me.. I would be grateful...
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?
Perhaps. Perhaps not. It depends on the internals of your platform.

"Memory alignment" is the recognition that the underlying platform may
have restrictions on where certain data types may be placed, in relation
to the space available in which to place them. Some platforms require
that certain data types be placed so that they start only on 'even
number addresses', or 'addresses that are divisible by 4' or some other
requirement. Some platforms have no requirements of this nature. The
requirement that limits the addresses that a data type can be placed at
is called "memory alignment".

The C standard specifies that malloc() and family will allocate memory
so that the address can be used for any data type. This means that
malloc() and family must be familiar with and able to accomodate the
restrictions the underlying platform place on the arrangement of data
items in memory. Not an easy task for portable code to accomplish, which
is why it is part of the implementation of a compiler.

B) so how can I write a function for example...
void * align_malloc(size_t size_of_memory_required, int
bit_alignement_required)
First off, your function will, by necessity, be specific to your
platform, and will not be portable.

Before you write such a function, you need to know
a) the mechanics of how your compiler arranges for a program to store
each data item. Typically, this will be an examination of the
assembly or machine code that your compiler generates, to determine
data type size, and store/load instruction type.
b) what restrictions your platform places on the placement of each data
type manipulated by the instructions generated by your compiler.

- From these, you can determine what machine restrictions your compiler
will have to contend with when accessing data types stored in an
align_malloc()'ed block of memory.

Once you have determined the restrictions, code your function to
allocate a block of memory, such that the block of memory starts at an
address that satisfies /all/ the alignment restrictions imposed by your
compiler on your platform. Then, return the address that the block
starts at.

Any help would be appreciated..

Thanks and advance

Dp

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCE5D1agVFX4UWr64RAqK2AKDnZcStlcuRODzkJ95P5r 9QOfq20gCginP0
y7+QXCh6gDBSU7KxuxQK4JU=
=SDVo
-----END PGP SIGNATURE-----
Nov 14 '05 #2


Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dhirendra Pal Singh wrote:
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
understand it clearly...? If anyone has a pointer on net please tell
me.. I would be grateful...
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?

Perhaps. Perhaps not. It depends on the internals of your platform.


So if I am using intel pentimum chipset, then would be correct? ie the
last 6 digits would be 0...
Actually someone asked me in an interview, so I was wondering what would
be best answere, asuming I am working on Intel Pentinum?

"Memory alignment" is the recognition that the underlying platform may
have restrictions on where certain data types may be placed, in relation
to the space available in which to place them. Some platforms require
that certain data types be placed so that they start only on 'even
number addresses', or 'addresses that are divisible by 4' or some other
requirement. Some platforms have no requirements of this nature. The
requirement that limits the addresses that a data type can be placed at
is called "memory alignment".

The C standard specifies that malloc() and family will allocate memory
so that the address can be used for any data type. This means that
malloc() and family must be familiar with and able to accomodate the
restrictions the underlying platform place on the arrangement of data
items in memory. Not an easy task for portable code to accomplish, which
is why it is part of the implementation of a compiler.

B) so how can I write a function for example...
void * align_malloc(size_t size_of_memory_required, int
bit_alignement_required)

First off, your function will, by necessity, be specific to your
platform, and will not be portable.

Before you write such a function, you need to know
a) the mechanics of how your compiler arranges for a program to store
each data item. Typically, this will be an examination of the
assembly or machine code that your compiler generates, to determine
data type size, and store/load instruction type.
b) what restrictions your platform places on the placement of each data
type manipulated by the instructions generated by your compiler.

- From these, you can determine what machine restrictions your compiler
will have to contend with when accessing data types stored in an
align_malloc()'ed block of memory.

Once you have determined the restrictions, code your function to
allocate a block of memory, such that the block of memory starts at an
address that satisfies /all/ the alignment restrictions imposed by your
compiler on your platform. Then, return the address that the block
starts at.
Any help would be appreciated..

Thanks and advance

Dp


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCE5D1agVFX4UWr64RAqK2AKDnZcStlcuRODzkJ95P5r 9QOfq20gCginP0
y7+QXCh6gDBSU7KxuxQK4JU=
=SDVo
-----END PGP SIGNATURE-----

Nov 14 '05 #3
Dhirendra Pal Singh wrote:
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
understand it clearly...? If anyone has a pointer on net please tell
me.. I would be grateful...
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?
Use your favorite search engine and search the newsgroups
for "alignment Thomas Matthews". I already posted a lengthy
dissertation on alignment.

B) so how can I write a function for example...
void * align_malloc(size_t size_of_memory_required, int
bit_alignement_required)
The C language does not require that malloc return a
pointer to an aligned area of memory.

The only guaranteed method is to allocate more memory than
required and create a pointer to the first aligned address
within that memory space.

Any help would be appreciated..

Thanks and advance

Dp


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Nov 14 '05 #4
Thomas Matthews <Th*************************@sbcglobal.net> writes:
Dhirendra Pal Singh wrote:

[...]
B) so how can I write a function for example...
void * align_malloc(size_t size_of_memory_required, int
bit_alignement_required)


The C language does not require that malloc return a
pointer to an aligned area of memory.

The only guaranteed method is to allocate more memory than
required and create a pointer to the first aligned address
within that memory space.


It guarantees that the allocated memory is suitably aligned for any
type of object. If you need stricter alignment than that for some
reason, you'll need to do something system-specific.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
On Thu, 17 Feb 2005 17:43:18 GMT, in comp.lang.c , Dhirendra Pal Singh
<ab**@sbcglobal.net> wrote:
Lew Pitcher wrote:
Dhirendra Pal Singh wrote:
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?


Perhaps. Perhaps not. It depends on the internals of your platform.


So if I am using intel pentimum chipset, then would be correct? ie the
last 6 digits would be 0...


Perhaps, perhaps not. You'd need to ask in a newsgroup which knew about
your hardware and OS.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #6
On Thu, 17 Feb 2005 17:43:18 +0000, Dhirendra Pal Singh wrote:


Lew Pitcher wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dhirendra Pal Singh wrote:
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
understand it clearly...? If anyone has a pointer on net please tell
me.. I would be grateful...
Some told me that for memory to be aligned, the last 6 digits of the
address should be zero? is this correct?

Perhaps. Perhaps not. It depends on the internals of your platform.


So if I am using intel pentimum chipset, then would be correct? ie the
last 6 digits would be 0...


That would be up to the specific compiler/library but it is unlikely. For
example 32 bit values are likely to be 4 byte aligned i.e. the address is
a multiple of 4 which would typically mean the last 2 bits are zero.
Actually someone asked me in an interview, so I was wondering what would
be best answere, asuming I am working on Intel Pentinum?


Alignment requirements are type-specific. x86 processors can access values
at any address but aligned access is faster so x86 compilers tend to
align objects.

Lawrence
Nov 14 '05 #7
In article <pa****************************@netactive.co.uk>
Lawrence Kirby <lk****@netactive.co.uk> wrote:
Alignment requirements are type-specific. x86 processors can access values
at any address ...
(except SSE instructions)
but aligned access is faster so x86 compilers tend to align objects.


Yes.

It is also worth noting (despite its off-topic-ness) that many
architectures have stricter "super-alignment" requirements for
types and/or operations that are not generated by C compilers.
Even the x86 recommends (I think) 128-byte alignment for some
multiprocessor synchronization operations (mutexes in MP systems).
(These are usually connected to cache line sizes, which tend to
run 16, 32, or 64 bytes today.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by benevilent | last post: by
13 posts views Thread by Michael B Allen | last post: by
7 posts views Thread by Dan Nilsen | last post: by
15 posts views Thread by Martin Jørgensen | last post: by
9 posts views Thread by Me | last post: by
71 posts views Thread by desktop | last post: by
11 posts views Thread by Jason | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.