how to get aliagned memory using malloc

  • Thread starter Dhirendra Pal Singh
  • Start date
D

Dhirendra Pal Singh

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
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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)

iD8DBQFCE5D1agVFX4UWr64RAqK2AKDnZcStlcuRODzkJ95P5r9QOfq20gCginP0
y7+QXCh6gDBSU7KxuxQK4JU=
=SDVo
-----END PGP SIGNATURE-----
 
D

Dhirendra Pal Singh

Lew said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



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?
 
T

Thomas Matthews

Dhirendra said:
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
 
K

Keith Thompson

Thomas Matthews said:
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.
 
M

Mark McIntyre

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.
 
L

Lawrence Kirby

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
 
C

Chris Torek

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top