?
Solved

What does memalign(,) do ?

Posted on 2003-02-27
3
Medium Priority
?
488 Views
Last Modified: 2012-05-04
What exactly will -

memalign(32,4);

do ?

memlign's first parameter ( 32 in the above case ) has to be a power fo two. Why ?

Kindly help
Thanks already.

Dhritiman
0
Comment
Question by:Dhritiman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
Linzer earned 100 total points
ID: 8032430
memalign(32, 4) returns a pointer to a block of allocated memory (4 bytes) whose address is a multiple of 32 bytes (it's the blocksize).  The blocksize has to be a power of two because when the system requires an object in memory to be aligned, it's always to powers of two. This can be seen as a consequence of pointers  being stored (as all integer values) with a binary representation : provided an object is aligned to 2^n bytes blocks, you need n bits less to describe its address than if it wasn't aligned.

Two remarks :
- pointers returned by memalign() should be passed to free() when not used anymore to prevent memory leaks;
- use of memalign is no longer recommended, since  standard memory allocation functions return aligned blocks.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10019041
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by Linzer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question