Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattiasgustavsson/b97e9e59e5e1742dcabff60179fcda67 to your computer and use it in GitHub Desktop.
Save mattiasgustavsson/b97e9e59e5e1742dcabff60179fcda67 to your computer and use it in GitHub Desktop.
Simple string allocation system, allowing for freeing all allocated strings in one go
#define STR_NEW( pool, str ) ( pool = memcpy( (char*) memcpy( malloc( ( str ? strlen( str ) : 0 ) + 1 + \
sizeof( char* ) ), &pool, sizeof( char* ) ) + sizeof( char*), str ? str : "", ( str ? strlen( str ) : 0 ) + 1 ) )
#define STR_FREE( pool ) while( pool ) { char* STR_FREE_TMP = ( pool - sizeof( char* ) ); \
pool = ( *(char**)STR_FREE_TMP ); free( STR_FREE_TMP ); }
//---------------------------
char* strpool = NULL;
char const* s1 = STR_NEW( strpool, "Test1" );
char const* s2 = STR_NEW( strpool, "Test2" );
STR_FREE( strpool );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment