fork download
  1. #include <stdio.h>
  2.  
  3. void dumpMem(void *start, int numBytes)
  4. {
  5. printf("memory at %p:", start);
  6. char *p = (char *)start;
  7. while((unsigned long)p%8){ p--; numBytes++;} // align to 8 byte boundary
  8. for(int i=0; i<numBytes; i++)
  9. {
  10. if( i%8 == 0 ) printf("\nAddr %p:", p+i);
  11. printf(" %02x", (unsigned int) (p[i] & 0xff));
  12. }
  13. putchar('\n');
  14. }
  15.  
  16. int len; // static allocation, protect them from stack overwrites
  17. char *from, *to;
  18.  
  19. int main(void)
  20. {
  21. unsigned int declStart = 0xaaaaaaaa; // marker
  22. int *x = (int *) 0xbbbbbbbbbbbbbbbb;
  23. double *y = (double *)0xcccccccccccccccc;
  24. unsigned int declEnd = 0xdddddddd; // marker
  25.  
  26. printf("Addr. of x: %p,\n of y: %p\n", &x, &y);
  27.  
  28. // This is all UB because the pointers are not
  29. // belonging to the same object. But it should
  30. // work on standard architectures.
  31. // All calls to dumpMem() therefore are UB, too.
  32.  
  33. // Thinking of it, I'd be hard-pressed to find
  34. // any defined behavior in this program.
  35. if( &declStart < &declEnd )
  36. {
  37. from = (char *)&declStart;
  38. to = (char *)&declEnd + sizeof(declEnd);
  39. }
  40. else
  41. {
  42. from = (char *)&declEnd;
  43. to = (char *)&declStart + sizeof(declStart);
  44. }
  45. len = to - from;
  46.  
  47. printf("len is %d\n", len);
  48. printf("Memory after initializations:\n");
  49. dumpMem(from, len);
  50.  
  51. x = (int *)&x;
  52. printf("\nMemory after assigning own address %p to x/*x: \n", &x);
  53. dumpMem(from, len);
  54.  
  55. *x = 3;
  56. printf("\nMemory after assigning 3 to x/*x: \n");
  57. dumpMem(from, len);
  58.  
  59. //print val of pointer
  60. printf("x as long: %d\n", (unsigned long)x);
  61.  
  62. y = (double *)&y;
  63. *y = 4.0;
  64. printf("\nMemory after assigning 4.0 to y/*y: \n");
  65. dumpMem(from, len);
  66.  
  67. printf("y as float: %f\n", y);
  68. printf("y as double: %lf\n", y); // prints 0.0
  69. printf("y, cast as double: %lf\n", *(double *)&y); // prints 4.0
  70. printf("y as unsigned int: 0x%x\n", y);
  71. printf("y as unsigned long: 0x%lx\n", y);
  72.  
  73. return 0;
  74. }
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
Addr. of x: 0xbfba00c4,
      of y: 0xbfba00c8
len is 16
Memory after initializations:
memory at 0xbfba00c0:
Addr 0xbfba00c0: aa aa aa aa bb bb bb bb
Addr 0xbfba00c8: cc cc cc cc dd dd dd dd

Memory after assigning own address 0xbfba00c4 to x/*x: 
memory at 0xbfba00c0:
Addr 0xbfba00c0: aa aa aa aa c4 00 ba bf
Addr 0xbfba00c8: cc cc cc cc dd dd dd dd

Memory after assigning 3 to x/*x: 
memory at 0xbfba00c0:
Addr 0xbfba00c0: aa aa aa aa 03 00 00 00
Addr 0xbfba00c8: cc cc cc cc dd dd dd dd
x as long: 3

Memory after assigning 4.0 to y/*y: 
memory at 0xbfba00c0:
Addr 0xbfba00c0: aa aa aa aa 03 00 00 00
Addr 0xbfba00c8: 00 00 00 00 00 00 10 40
y as float: -0.101574
y as double: -0.101574
y, cast as double: 4.000000
y as unsigned int: 0x0
y as unsigned long: 0x0