Share this post

Link to post

Share on other sites

If you want to find out how big a string is, use strlen(). This returns the number of characters in the string, not including the terminating zero.To concatenate one string with a second, and limit the number of characters to copy, use strncat(). So you could use a limit like (MAX_LEN-strlen(string)-1), if MAX_LEN is the size of the character array (aString[MAX_LEN]).

A lot of the string manipulation routines are str[n][action]. If the n is there, there will be a parameter to limit the number of characters.

0

Share this post

Link to post

Share on other sites

By "overflow", I assume you mean writing outside the boundaries of your array. One way to prevent this is to place sentinels on the boundaries of your array. By sentinel I just mean a unique value; when you free your array, you can check to see if it's been overwritten. The code below will use 3 S's as a sentinel:

A few things: Out-of-bounds checking is not necessary once your program is complete (release builds). Since you're only doing the checking for debug builds, it doesn't really matter how much memory you waste on the sentinel -- use 16 bytes, 256 bytes, whatever. Also, yes, I was too lazy to actually define SetMemBlockSize and GetMemBlockSize.