Click here to Skip to main content
15,891,933 members
Articles / Programming Languages / C++

Allocate your dynamic strings on the stack

Rate me:
Please Sign up or sign in to vote.
2.50/5 (3 votes)
5 Jun 20056 min read 76.6K   288   8  
An article about stack-allocated dynamic strings in C++.
//Misc
//Copyright Olivier Lombart 2004-2005

//Remark:
//these things were copied here for the sample to work properly;
//they are not part of a single file in my own source code (i.e. I am not that messy ;)).



//---string stuff

typedef TCHAR xchar;
#define _x(txt)			__T(txt)
typedef const xchar* cxstr;
typedef xchar* xstr;
typedef const wchar_t* cwstr;


#define tstrlen			_tcslen
#define tstrcmp			_tcscmp

#define VSNPRINTF	_vsntprintf

//returns the number of characters in a buffer
//(use this instead of sizeof to get the character length of a xchar buffer)
#define tlenof(zBuf)	((void)(1/(sizeof(zBuf)-sizeof(xchar*))),	\
					sizeof(zBuf)/sizeof(xchar))
//rem: the trick above with 1/(sizeof(zBuf)-sizeof(xchar*)) is to prevent the common mistake
//of passing an xchar* instead of an xchar array. This macro would generate a
//"divide or mod by zero" error if called with a xchar* size parameter.
//note that tlenof would return a meaningless value if called on an xchar*.
//Unfortunately, this trick forbids to use tlenof for a buffer of 4 xchar
//(in single byte char mode) or 2 xchar (in double byte mode). But I think this is worth the 
//burden. If you do need a buffer this small, please use sizeof(yourBuf)/sizeof(xchar) instead.



//---debug stuff

#include <assert.h> //assert

#ifdef _DEBUG
	//code produced only in debug:
	#define TDBG(act)		act
	#define TASSERT(exp)	assert(exp)
	#define TASSERTC(exp,com)	assert((com,exp))
#else 
	//---release version => no code produced
	#define TDBG(act)
	#define TASSERT(exp)
	#define TASSERTC(exp,com)
#endif //#ifdef _DEBUG


#define tchkptr_r(ptr,size)		(!IsBadReadPtr(ptr,size))
#define tchkptr_w(ptr,size)		(!IsBadWritePtr(ptr,size))
#define tchkptr_rw(ptr,size)	(tchkptr_r(ptr,size) && tchkptr_r(ptr,size))

//the upper case ones are active only in debug mode
#define TCHECKPTR(ptr,size)			TASSERT(tchkptr_rw(ptr,size))
#define TCHECKPTR_R(ptr,size)		TASSERT(tchkptr_r(ptr,size))

inline void  tmemmove(void* dest,const void* src,size_t size)		{TASSERT(tchkptr_r(src,size) && tchkptr_w(dest,size)); memmove(dest,src,size);}
inline void  tmemcpy(void* dest,const void* src,size_t size)		{TASSERT(tchkptr_r(src,size) && tchkptr_w(dest,size)); memcpy(dest,src,size);}



//---Chrono stuff

#define TCHRONO_START(name)			clock_t name; name=clock();
#define TCHRONO_STOP(name)			name=(clock_t)(((clock()-name)/(double)CLOCKS_PER_SEC)*1000);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Japan Japan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions