Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char stack[] = "hello";
- char *heap = "hello";
- char *heap_string_malloc = malloc(5);
- heap_string_malloc = "hello";
- printf("Address of stack[0]: %pn", stack);
- printf("Address of heap[0]: %pn", heap);
- printf("Address of heap_string_malloc[0]: %pn", heap_string_malloc);
- Address of stack[0]: 0x7fff8b0b85b0
- Address of heap[0]: 0x400760
- Address of heap_string_malloc[0]: 0x400760
- // Allocate 6 bytes in the stack and store "hello" in them
- char stack[] = "hello";
- // Allocate pointer on the stack and point it to a static, read-only buffer
- // containing "hello"
- char *heap = "hello";
- // Malloc 5 bytes (which isn't enough to hold "hello" due to the NUL byte)
- char *heap_string_malloc = malloc(5);
- // Reset heap_string_malloc to point to a static buffer; memory leak!
- heap_string_malloc = "hello";
- char *heap = "hello";
- const char *heap = "hello";
- char *heap_string_malloc = malloc(5);
- heap_string_malloc = "hello";
- char stack[] = "hello";
- char *heap = "hello";
- Item Address 00 01 02 03
- ---- ------- -- -- -- --
- "hello" 0x400b70 68 65 6c 6c hell
- 0x400b74 6f 00 22 68 o."h
- stack 0x7fffb00c7620 68 65 6c 6c hell
- 0x7fffb00c7624 6f 00 00 00 o...
- heap 0x7fffb00c7618 70 0b 40 00 p.@.
- 0x7fffb00c761c 00 00 00 00 ....
- *heap 0x400b70 68 65 6c 6c hell
- 0x400b74 6f 00 22 68 o."h
- heap = malloc( sizeof *heap * strlen( "hello" + 1 ));
- strcpy( heap, "hello" );
- Item Address 00 01 02 03
- ---- ------- -- -- -- --
- "hello" 0x400b70 68 65 6c 6c hell
- 0x400b74 6f 00 22 68 o."h
- stack 0x7fffb00c7620 68 65 6c 6c hell
- 0x7fffb00c7624 6f 00 00 00 o...
- heap 0x7fffb00c7618 10 10 50 00 ..P.
- 0x7fffb00c761c 00 00 00 00 ....
- *heap 0x501010 68 65 6c 6c hell
- 0x501014 6f 00 00 00 o...
- #ifndef DUMPER_H
- #define DUMPER_H
- /**
- * Dumps a memory map to the specified output stream
- *
- * Inputs:
- *
- * names - list of item names
- * addrs - list of addresses to different items
- * lengths - length of each item
- * count - number of items being dumped
- * stream - output destination
- *
- * Outputs: none
- * Returns: none
- */
- void dumper(char **names, void **addrs, size_t *lengths, size_t count, FILE *stream);
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #include "dumper.h"
- /**
- * Dumps a memory map to the specified output stream
- *
- * Inputs:
- *
- * names - list of item names
- * addrs - list of addresses to different items
- * lengths - length of each item
- * count - number of items being dumped
- * stream - output destination
- *
- * Outputs: none
- * Returns: none
- */
- void dumper(char **names, void **addrs, size_t *lengths, size_t count, FILE *stream)
- {
- size_t i;
- int maxlen = 15;
- for ( size_t j = 0; j < count; j++ )
- {
- if (strlen(names[j]) > maxlen && strlen(names[j]) < 50)
- maxlen = strlen(names[j]);
- }
- fprintf(stream,"%*s%15s%5s%5s%5s%5sn", maxlen, "Item", "Address", "00", "01",
- "02", "03");
- fprintf(stream,"%*s%15s%5s%5s%5s%5sn", maxlen, "----", "-------", "--", "--",
- "--", "--");
- for (i = 0; i < count; i++)
- {
- size_t j;
- char *namefield = names[i];
- unsigned char *p = (unsigned char *) addrs[i];
- for (j = 0; j < lengths[i]; j+=4)
- {
- size_t k;
- fprintf(stream,"%*.*s", maxlen, maxlen, namefield);
- fprintf(stream,"%15p", (void *) p);
- for (k = 0; k < 4; k++)
- {
- fprintf(stream,"%3s%02x", " ", p[k]);
- }
- fprintf(stream, " ");
- for ( k = 0; k < 4; k++)
- {
- if (isgraph(p[k]))
- fprintf(stream,"%c", p[k]);
- else
- fprintf(stream, ".");
- }
- fputc('n', stream);
- namefield = " ";
- p += 4;
- }
- fputc('n', stream);
- }
- }
- #include <stdio.h>
- #include "dumper.h"
- int main(void)
- {
- int x = 0;
- double y = 3.14159;
- char foo[] = "This is a test";
- void *addrs[] = {&x, &y, foo, "This is a test"};
- char *names[] = {"x", "y", "foo", ""This is a test""};
- size_t lengths[] = {sizeof x, sizeof y, sizeof foo, sizeof "This is a test"};
- dumper(names, addrs, lengths, 4, stdout);
- return 0;
- }
- char *heap = "hello";
- heap_string_malloc = "hello";
- memcpy(heap_string_malloc, "hello", 5);
- char stack[] = "hello";
- char *heap = "hello";
- char *heap_string_malloc = malloc(5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement