int strlen(char* _String)
{
unsignedint iLoop = 0;
while(true)
{
if (_String[iLoop] == 0)
{
return iLoop;
break;
}
iLoop++;
}
}
char* stringappend(char* _destination, char* _append)
{
char* cdestination = (char*)malloc((strlen(_destination) + strlen(_append)) * sizeof(char));
for (int i = 0; i < strlen(_destination); i++)
{
cdestination[i] = _destination[i];
}
for (int i = 0; i < strlen(_append); i++)
{
cdestination[strlen(_destination) + i] = _append[i];
}
cdestination[strlen(_destination) + strlen(_append)] = 0;
return cdestination;
}
When I call stringappend() using below... why does it error on the free() line?
1 2 3
char* g = stringappend("Key", "1");
AddQueryString(g, "Value1"); //this is a function not important here
free(g);
I assume there is something wrong with my memory allocation (as a vb developer, this is what I am currently trying to get my head around). Is there something wrong with allocating the memory in a function and then freeing it outside of the function?
* sizeof(char)) is pointless. sizeof(char) is guaranteed to be 1. (your paranthesises are also wrong in the context... you are multiplying the void* malloc returns there lol)
Your strlen function is also wrong, as it also counts the '\0' character- it shouldn't do that.
cdestination[strlen(_destination) + i] you keep calling the strlen function here for no reason, it would have been faster if you just called strlen once and stored the result into an int.
Aside from that, are you trying to learn C or C++? If you want to learn C++, you can leave all that C stuff like malloc or free for now - it's only required for compatibility to C.
No wait you are right, it's correct. I misread one line in it. It should take a const char* instead of a char* though.
Sorry, I don't know anything about the Arduino platform. It would help though if you told us what error you are getting, if AddQueryString does anything with g.
By making the argument constant, you are guaranteeing the user that strlen will not change the string it is being passed. Other than that, you also couldn't pass constant strings to the function without the compiler complaining (at the very least with all warnings turned on) if you passed a constant string.
Identifiers beginning with an underscore and a capital letter are reserved. Additionally, passing a null string and a zero-length, null-terminated string are two different things--strlen above does not check this.
"By making the argument constant, you are guaranteeing the user that strlen will not change the string it is being passed. Other than that, you also couldn't pass constant strings to the function without the compiler complaining (at the very least with all warnings turned on) if you passed a constant string."
But this is why I am writing the function? I don't want to pass constants, I want to pass variables. The compiler complained UNTIL I wrote this function.
Guarantee the user? The user is me. I know I won't change it. Sorry, I don't get your thinking. Please tell me if its something I am missing.
"Identifiers beginning with an underscore and a capital letter are reserved. Additionally, passing a null string and a zero-length, null-terminated string are two different things--strlen above does not check this."
But this is why I am writing the function? I don't want to pass constants, I want to pass variables.
You don't want to use strlen("abc");? "abc" is a constant string. And:
Guarantee the user? The user is me. I know I won't change it. Sorry, I don't get your thinking. Please tell me if its something I am missing.
This line of thinking works in small test programs like this, but you don't have the complete overview of what is happening at any second in a 100000 line program.
You don't want to use strlen("abc");? "abc" is a constant string.
Nope.
This line of thinking works in small test programs like this, but you don't have the complete overview of what is happening at any second in a 100000 line program.
Guarantee the user? The user is me. I know I won't change it. Sorry, I don't get your thinking. Please tell me if its something I am missing.
If you know you won't change it, then make it const. There's no reason not to.
If you're not changing the passed string, using a const pointer will work in all cases a non-const pointer will, and some others where a non-const pointer won't work.