0

Write an aligned malloc & free function which takes number of bytes and aligned byte (which is always power of 2) and returns the memory address divisible by the number alligned byte.

Ex. align_malloc (1000,128);

it will return memory address multiple of 128 of the size 1000.

aligned_free(); 

it will free memory allocated by align_malloc.

For allocating function, I wrote the following code:

void * allocatemyfunc (size_t bytestobeallocated, size_t allignment)
{
  void * p1;
  void * p2;

  p1 = (void*)malloc(bytestobeallocated+allignment);
  if ( p1 == NULL )
    return 'error';
  else
  {
      size_t addr = bytestobeallocated + allignment;
      p2 = (void*)addr-(addr%allignment);
      return p2;
  }
}

This seems to be the appropriate solution for alligned allocation. (I might be wrong, correct me if I am).

How do I write the alligned free function? (this will basically free all the memory allocated)

7
  • Unrelated, casting malloc to void * is senseless. Related, your alignment calculation is incorrect. To round to a nearest boundary you add the rounding factor less-one, then do your modulo and readd work. And you seem to be leaving no path back to the original malloc result to later free when finished. free()-ing p2 is obviously not going to work. Commented Jun 7, 2015 at 17:39
  • "Write an aligned malloc & free function" are you telling us to do this? Or are you saying that someone - perhaps a teacher - asked you to do this? Commented Jun 7, 2015 at 17:42
  • 1
    When you used the debugger, which lines are causing the issue? You did use a debugger before posting? Commented Jun 7, 2015 at 17:43
  • I was reading a book that asked me to do this. I tried doing it on my own which I have already written in the question. All I am asking from you is to check if its correct or not? This is not a homework question. If it would have been, I wouldn't have posted it here. I would have probably gone and consulted my teacher in the first place. Commented Jun 7, 2015 at 17:44
  • 1
    This is a Q&A, not a helpdesk. "Questions" on issues without prior debugging are almost always off-topic, by virtue of how they are constructed. Commented Jun 7, 2015 at 17:46

2 Answers 2

1

Your align malloc is erroneous, you can do something like this to align a pointer.

uint8_t* ptr = (uint8_t*)malloc(N + alignment);
....
uint8_t offset = alignment - ((uintptr_t)ptr % alignment);
ptr += offset;

Now you have a pointer that has offset bytes of free space to the front, you store the offset there.

*((uint8_t*)ptr - 1) = offset;

To free the ptr you decrement offset to get to the beginning

uint8_t offset = *((uint8_t*)ptr - 1);
free((uint8_t*)ptr - offset);
Sign up to request clarification or add additional context in comments.

7 Comments

That is making an assumption about integer data type sizes and also the amount of alignment he had to add. For example what if it turns out that offset is actually 0?
I am making assumption that alignment is small, offset cannot be zero assuming valid value for ptr and alignment, I omitted those error checks.
The offset can be zero if malloc's result happens to already be aligned to alignment. Although I did observe that you specifically typed offset as uint8_t which does eliminate one of my complaints about integer data type sizes.
In that case wouldn't offset be alignment?
Yes, yes it would be. That's my mistake. That does raise the related issue of aligning pointers that are already aligned :P
|
0

This is not exactly a dup question, but see my answer here for help

What is the recommended way to align memory in C++11

The methodology is to be able to find the beginning of the block from the returned memory by referencing the memory before the returned memory

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.