my_alloca.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #ifndef _MY_ALLOCA_H_ #define _MY_ALLOCA_H_ #include <malloc.h> __declspec(thread) extern size_t strdupa_len; __declspec(thread) extern char* strdupa_p; #define strdupa(_str) \ ( \ strdupa_len = strlen(_str), \ strdupa_p = (char*)_alloca(strdupa_len + 1), \ memcpy(strdupa_p, _str, strdupa_len + 1), \ strdupa_p \ ) #endif |
my_alloca.c
1 2 3 | #include "my_alloca.h" __declspec(thread) size_t strdupa_len; __declspec(thread) char* strdupa_p; |
example.c
1 2 3 4 5 6 | #include "my_alloca.h" int main() { char* p = strdupa("hi"); } |