Member Avatar

Hello, I was recently looking through some of my code and found what I believe to be a memory leak. It is in a function that appends two strings and I am not sure how to resolve it. Here is the function: const char *strapp(const char *str,const char *s) { int size,strSize,sSize; for (strSize=0;str[strSize];++strSize); for (sSize=0;s[sSize];++sSize); size=strSize+sSize; char *ret=new char[size+1]; for (size_t i=0; i<strSize; ++i) ret[i]=str[i]; for (size_t i=0; i<sSize; ++i) ret[strSize+i]=s[i]; ret[size]=0; return ret; } Note that this includes a `new` operator but would leave it up to the untrustworthy user of the function to `delete` the resulting pointer …

Member Avatar
+0 forum 7 42
Member Avatar

I know that this [ICODE]#define QUOTE(X) #X[/ICODE] turns X into a c-string version of whatever is passed to it. my question is if there is a way to do this in reverse ie: [ICODE]#define DEQUOTEANDCALLFUNCTIONORCLASSWITHGIVENNAME(X) (X#)()[/ICODE] is this possible?

Member Avatar
+1 forum 3 4
Member Avatar

I've been coding in c++ for a while now, and I use string variables quite a lot. One day, I stumbled upon a char* which can also be used to "store" a string of characters, and it's been bothering me ever since that I don't really know what a string is (and by "string", I don't mean a line of characters, I mean the <cstring> string). Is it a class? A struct? A typedef? I would really like someone to give me an intermediate explanation of the inner workings of string variables. Thanks a lot

Member Avatar
+0 forum 2 3

The End.