You are copying the integer value (55) into the pointer int_out
. Whatever lies at (the next 4 to 8 bytes at) address 55, it is unlikely to have the numerical value 55. Instead, try
int int_in = 55;
int int_out;
store ( storage_int, &int_in, sizeof(int));
retrieve ( &int_out, storage_int, sizeof(int));
assert ( 55 == int_out);
Another problem with your code: in the char_in
/char_out
parts, you are only ever copying pointer values. This works (in this case), but is not likely what you really intended, or is it? If you really want to store the pointer, you can skip the store
and retrieve
part:
char* str_in = "hello, world"; // Original value
void* generic_ptr = str_in; // ... a pointer to which is stored in some data structure
char* str_out = generic_ptr; // ... which can later be retrieved
will work as well. On the other hand: if you intend to store the actual content of the string, you have to copy it (like, with _strdup
as in your second example), and save the pointer to the copy of the string:
char* str_in = _strdup("copy me");
void* generic_ptr = str_in; // Gets stored in your data structures