Advertisement
sapitando

TDM-GCC 5.10 64 bit - Variadic functions

Sep 9th, 2016
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. /* Autor : Tiago Portela
  2.    Email : sapitando@gmail.com
  3.    Sobre : Compilado com TDM-GCC 5.10 64 bit. Tentando entender a disposição na memória dos argumentos da função variádica,
  4.            cada compilador(e sua própria versão) implementa a sua maneira própria.
  5.    Obs : Apenas tentando aprender algoritimos, sozinho, por hobby. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdint.h>
  10. #include <stdarg.h>
  11. #include <conio.h>
  12.  
  13.  
  14. void Myprintf(const char* format,...)
  15. {
  16.     va_list args;
  17.     args = (va_list)(&format + 1); // va_start(args, format);
  18.     void *p = (void*)args;
  19.  
  20.     printf("%p -> %p = %s\n", (void*)(&format), format, format);
  21.     printf("%p -> %p = %s\n", (void*)(&format + 1), (void*)*(&format + 1), *(&format + 1));
  22.     printf("%p -> %p = %s\n", (void*)(&format + 2), (void*)*(&format + 2), *(&format + 2));
  23.     printf("%p -> %p = %lf\n", (void*)(&format + 3), (void*)*(&format + 3), (double)**(long double**)(&format + 3));
  24.     printf("%p = %lf\n", (void*)(&format + 4), *(double*)(&format + 4));
  25.     printf("%p = %i\n", (void*)(&format + 5), *(int*)(&format + 5));
  26.     printf("%p = %p = %u\n", (void*)(&format + 6), *(void**)(&format + 6), **(int**)(&format + 6) );
  27.     printf("\n\n");
  28.  
  29.     printf("%p = %u\n", (void*)((void**)args + 4),  *(unsigned int*)(args + 4 * sizeof(uintptr_t)));
  30.     printf("%p = %p = %u\n", (void*)((void**)args + 5), *((void**)args + 5), **((unsigned int**)args + 5));
  31.     printf("\n\n");
  32.  
  33.     vfprintf(stdout, format, (va_list)p);
  34.     va_end(args);
  35.     printf("\n");
  36. }
  37.  
  38.  
  39. int main(void){
  40.     long double ldCount = 99.50;
  41.     double lfCount = 250.125;
  42.     unsigned int uiCount = 199;
  43.     Myprintf("AAAAAAAAAA %s %s %G %lf %u %p", "BBBBBBBBBB", "CCCCCCCCCC", ldCount, lfCount, uiCount, &uiCount);
  44.     getch();
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement