Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. char stack[] = "hello";
  2. char *heap = "hello";
  3.  
  4. char *heap_string_malloc = malloc(5);
  5. heap_string_malloc = "hello";
  6.  
  7. printf("Address of stack[0]: %pn", stack);
  8. printf("Address of heap[0]: %pn", heap);
  9. printf("Address of heap_string_malloc[0]: %pn", heap_string_malloc);
  10.  
  11. Address of stack[0]: 0x7fff8b0b85b0
  12. Address of heap[0]: 0x400760
  13. Address of heap_string_malloc[0]: 0x400760
  14.  
  15. // Allocate 6 bytes in the stack and store "hello" in them
  16. char stack[] = "hello";
  17.  
  18. // Allocate pointer on the stack and point it to a static, read-only buffer
  19. // containing "hello"
  20. char *heap = "hello";
  21.  
  22. // Malloc 5 bytes (which isn't enough to hold "hello" due to the NUL byte)
  23. char *heap_string_malloc = malloc(5);
  24.  
  25. // Reset heap_string_malloc to point to a static buffer; memory leak!
  26. heap_string_malloc = "hello";
  27.  
  28. char *heap = "hello";
  29.  
  30. const char *heap = "hello";
  31.  
  32. char *heap_string_malloc = malloc(5);
  33. heap_string_malloc = "hello";
  34.  
  35. char stack[] = "hello";
  36.  
  37. char *heap = "hello";
  38.  
  39. Item Address 00 01 02 03
  40. ---- ------- -- -- -- --
  41. "hello" 0x400b70 68 65 6c 6c hell
  42. 0x400b74 6f 00 22 68 o."h
  43.  
  44. stack 0x7fffb00c7620 68 65 6c 6c hell
  45. 0x7fffb00c7624 6f 00 00 00 o...
  46.  
  47. heap 0x7fffb00c7618 70 0b 40 00 p.@.
  48. 0x7fffb00c761c 00 00 00 00 ....
  49.  
  50. *heap 0x400b70 68 65 6c 6c hell
  51. 0x400b74 6f 00 22 68 o."h
  52.  
  53. heap = malloc( sizeof *heap * strlen( "hello" + 1 ));
  54. strcpy( heap, "hello" );
  55.  
  56. Item Address 00 01 02 03
  57. ---- ------- -- -- -- --
  58. "hello" 0x400b70 68 65 6c 6c hell
  59. 0x400b74 6f 00 22 68 o."h
  60.  
  61. stack 0x7fffb00c7620 68 65 6c 6c hell
  62. 0x7fffb00c7624 6f 00 00 00 o...
  63.  
  64. heap 0x7fffb00c7618 10 10 50 00 ..P.
  65. 0x7fffb00c761c 00 00 00 00 ....
  66.  
  67. *heap 0x501010 68 65 6c 6c hell
  68. 0x501014 6f 00 00 00 o...
  69.  
  70. #ifndef DUMPER_H
  71. #define DUMPER_H
  72.  
  73. /**
  74. * Dumps a memory map to the specified output stream
  75. *
  76. * Inputs:
  77. *
  78. * names - list of item names
  79. * addrs - list of addresses to different items
  80. * lengths - length of each item
  81. * count - number of items being dumped
  82. * stream - output destination
  83. *
  84. * Outputs: none
  85. * Returns: none
  86. */
  87. void dumper(char **names, void **addrs, size_t *lengths, size_t count, FILE *stream);
  88.  
  89. #endif
  90.  
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <ctype.h>
  94. #include <string.h>
  95.  
  96. #include "dumper.h"
  97.  
  98. /**
  99. * Dumps a memory map to the specified output stream
  100. *
  101. * Inputs:
  102. *
  103. * names - list of item names
  104. * addrs - list of addresses to different items
  105. * lengths - length of each item
  106. * count - number of items being dumped
  107. * stream - output destination
  108. *
  109. * Outputs: none
  110. * Returns: none
  111. */
  112. void dumper(char **names, void **addrs, size_t *lengths, size_t count, FILE *stream)
  113. {
  114. size_t i;
  115. int maxlen = 15;
  116.  
  117. for ( size_t j = 0; j < count; j++ )
  118. {
  119. if (strlen(names[j]) > maxlen && strlen(names[j]) < 50)
  120. maxlen = strlen(names[j]);
  121. }
  122.  
  123. fprintf(stream,"%*s%15s%5s%5s%5s%5sn", maxlen, "Item", "Address", "00", "01",
  124. "02", "03");
  125. fprintf(stream,"%*s%15s%5s%5s%5s%5sn", maxlen, "----", "-------", "--", "--",
  126. "--", "--");
  127.  
  128. for (i = 0; i < count; i++)
  129. {
  130. size_t j;
  131. char *namefield = names[i];
  132. unsigned char *p = (unsigned char *) addrs[i];
  133. for (j = 0; j < lengths[i]; j+=4)
  134. {
  135. size_t k;
  136.  
  137. fprintf(stream,"%*.*s", maxlen, maxlen, namefield);
  138. fprintf(stream,"%15p", (void *) p);
  139. for (k = 0; k < 4; k++)
  140. {
  141. fprintf(stream,"%3s%02x", " ", p[k]);
  142. }
  143. fprintf(stream, " ");
  144. for ( k = 0; k < 4; k++)
  145. {
  146. if (isgraph(p[k]))
  147. fprintf(stream,"%c", p[k]);
  148. else
  149. fprintf(stream, ".");
  150. }
  151. fputc('n', stream);
  152. namefield = " ";
  153. p += 4;
  154. }
  155. fputc('n', stream);
  156. }
  157. }
  158.  
  159. #include <stdio.h>
  160.  
  161. #include "dumper.h"
  162.  
  163. int main(void)
  164. {
  165. int x = 0;
  166. double y = 3.14159;
  167. char foo[] = "This is a test";
  168.  
  169. void *addrs[] = {&x, &y, foo, "This is a test"};
  170. char *names[] = {"x", "y", "foo", ""This is a test""};
  171. size_t lengths[] = {sizeof x, sizeof y, sizeof foo, sizeof "This is a test"};
  172.  
  173. dumper(names, addrs, lengths, 4, stdout);
  174.  
  175. return 0;
  176. }
  177.  
  178. char *heap = "hello";
  179.  
  180. heap_string_malloc = "hello";
  181.  
  182. memcpy(heap_string_malloc, "hello", 5);
  183.  
  184. char stack[] = "hello";
  185.  
  186. char *heap = "hello";
  187.  
  188. char *heap_string_malloc = malloc(5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement