Task

Implement the function align_me16

  • Write a function that takes a void* pointer representing a memory address
  • If the address is already aligned to a 16-byte boundary (the address is a multiple of 16), return the original pointer
  • if not, return a pointer to the next higher address that is aligned to a 16-byte boundary

Function Signature

// Given a void* pointer, return the same pointer if it is 16-byte aligned, otherwise return the next higher 16-byte aligned address.
void* align_me16(void* ptr);

Example

Input AddressOutput Address
0x10000x1000
0x110x20
0x2f0x30
0x123450x12350
0x7fffffff0x80000000

Code

#include <stdint.h>
 
void* align_me16(void* ptr){
	uint64_t a = (uint64_t) ptr;
	int dif = a % 16;
	if (dif == 0) {return ptr;}
	return (ptr + 16 - dif);
}