[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

What does memalign(,) do ?

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
Dhritiman
Asked:
Dhritiman
1 Solution
 
LinzerCommented:
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
 
jmcgOwnerCommented:
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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now