473,324 Members | 2,456 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,324 software developers and data experts.

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

Similar topics

6
by: benevilent | last post by:
Hey, I'm trying to debug the memory allocation in an embedded use of the Python interpreter. The longer I leave my program running the more memory it consumes. The total number of objects in...
13
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
9
by: Me | last post by:
Hi, I ran into a malloc problem but I can't find the solution. I try to read a file into a variable with malloc like this: BYTE *lcdata; lcdata = malloc(fsize*sizeof(BYTE));
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
11
by: Jason | last post by:
I've inherited some old C code that I've been tasked to convert to C++. I've been running into some problems with memory allocation. First of all, is there any problems using "malloc" in C++? I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.