Created
November 21, 2014 12:35
-
-
Save kevinlynx/c51a9f4c74fcd6680679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <map> | |
#include <vector> | |
#include <list> | |
void ds_str_i(void *p) { | |
char **raw = (char**)p; | |
char *s = *raw; | |
size_t len = *(size_t*)(s - sizeof(size_t) * 3); | |
printf("str: %s (%zd)\n", s, len); | |
} | |
size_t ds_str() { | |
std::string s = "hello"; | |
ds_str_i(&s); | |
return s.size(); | |
} | |
template <typename T> | |
void ds_vec_i(void *p) { | |
T *start = *(T**)p; | |
T *finish = *(T**)((char*)p + sizeof(void*)); | |
T *end_storage = *(T**)((char*)p + 2 * sizeof(void*)); | |
printf("vec size: %ld, avaiable size: %ld\n", finish - start, end_storage - start); | |
} | |
size_t ds_vec() { | |
std::vector<int> vec; | |
vec.push_back(0x11); | |
vec.push_back(0x22); | |
vec.push_back(0x33); | |
ds_vec_i<int>(&vec); | |
return vec.size(); | |
} | |
size_t ds_map() { | |
std::map<std::string, int> imap; | |
imap["abc"] = 0xbbb; | |
return imap.size(); | |
} | |
#define NEXT(ptr, T) do { \ | |
void *n = *(char**)ptr; \ | |
T val = *(T*)((char**)ptr + 2); \ | |
printf("list item %p val: 0x%x\n", ptr, val); \ | |
ptr = n; \ | |
} while (0) | |
template <typename T> | |
void ds_list_i(void *p) { | |
void *ptr = *(char**)p; | |
NEXT(ptr, T); | |
NEXT(ptr, T); | |
NEXT(ptr, T); | |
} | |
size_t ds_list() { | |
std::list<int> lst; | |
lst.push_back(0x11); | |
lst.push_back(0x22); | |
lst.push_back(0x33); | |
ds_list_i<int>(&lst); | |
return lst.size(); | |
} | |
int main() | |
{ | |
ds_str(); | |
ds_map(); | |
ds_vec(); | |
ds_list(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment