Hi all..
I was looking for a simple substring type routine.. and I found one on the internet and it's nice.
I'm not much of a C programmer, and it has a malloc in the middle of it, and mallocs scare me, incase they are memory leaks.
can I ask..
a) is it ?!
b) if it is, how do I stop it being one. (with a free somewhere? but where?)
at the minute I'm using it to trim a string to be printed on an LCD, soon it'll be a "teletyper"
(when I put a loop on the call to it.)
How I call it:
void teletype(char* text, int row) {
lcd.locate(0,row);
lcd.printf(" ");
lcd.locate(0,row);
//loop will go here
lcd.printf(left(text,2));
}
The function with the malloc;
char* left(string Source,int NewLen) {
char* result;
char* temp;
if (NewLen <=0)
NewLen =1; /* Minimum length */
result = (char*)malloc(NewLen + 1); /* +1 = DFTTZ! */
*result=' '; /* default for unassigned strings */
temp=result;
if (Source && *Source) /* don't copy an empty string */
{
while (NewLen-- >0)
*temp++=*Source++;
}
else temp++;
*temp='\0';
return result;
}
Hi all..
I was looking for a simple substring type routine.. and I found one on the internet and it's nice.
I'm not much of a C programmer, and it has a malloc in the middle of it, and mallocs scare me, incase they are memory leaks.
can I ask..
a) is it ?! b) if it is, how do I stop it being one. (with a free somewhere? but where?)
at the minute I'm using it to trim a string to be printed on an LCD, soon it'll be a "teletyper" (when I put a loop on the call to it.)
How I call it:
The function with the malloc;