XAgent-Smith

Align desired multiple addressed memory allocation

Nov 23rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. /*Align memory of desired multiple number address (except prime ) allocated*/
  2. #include <stdlib.h>
  3. #include<stdio.h>
  4.  
  5. void  *allign_alloc(size_t allign ,size_t bytes)
  6. {
  7.    size_t *book = NULL ;
  8.  const  size_t total_size = bytes+allign+sizeof(size_t );
  9.  
  10.   char *data = (char *)malloc(sizeof(char)*total_size) ;
  11.  
  12.    const void * const data_row = data  ;
  13.     data = data + sizeof(size_t) ;
  14.  
  15.   const size_t offset = allign - ((size_t)data%allign)  ;
  16.  
  17.     data+=offset  ;
  18.    
  19.     book = (size_t*)data - sizeof(size_t );
  20.  
  21.     *book = (size_t )data_row ;
  22.  
  23.   return data ;
  24.    
  25. }
  26. int main()
  27. {
  28. int *ptr = allign_alloc(7,30) ;
  29.  
  30. printf("\n%s\n%u",(char*)((((size_t)ptr%7)==0)?"Yes":"NO"),(unsigned)ptr) ;
  31.  
  32.  
  33.  getchar();
  34. free((void*)*(ptr-sizeof(size_t))) ;
  35. return 0 ;
  36. }
Add Comment
Please, Sign In to add comment