I've been having problems with the overload of a *char. After some amount of text, it starts to add garbage characters in it. Here's an example of my code:
Love o
Love o o
Love o o o
Love o o o o
Love o o o o o
Love o o o o oï@}ƒo
Love o o o o oï@}ƒo o
Love o o o o oï@}ƒo o o
Love o o o o oï@}ƒo o o o
Love o o o o oï@}ƒo o o o o
The function simply creates the char*textResult and assigns text to it character-by-character, just like how it's done with an array.
Looks like when either the prefix or sulfix (or both), used in linkText, exceed 15 chars, it returns garbage letters. Could someone help me to get this solved?
This seems to be a real convoluted (and flawed) way of joining two string together.
Here are some wrong things:
1. char *textResult=newchar;
This only allocates space for ONE char - which means that this : textResult[textOffset[0]+textOffset[1]] = textArray[i][textOffset[i]]; is writing to OUT OF BOUNDS area (causing heap corruption).
2. The function char *linkText (constchar *prefix, constchar *sulfix) returns a pointer
to the memory allocated by new .
This memory is never deleted causing memory leaks.
Thank you kindly for taking your time to post here. I think I don't have to state yet that I'm a beginner after showing the mistakes that I made.
@helios, your solution is by far neater, but I really want to do it the hard way instead of using C functions like strcat because I think it's a good (and fun) way of learning programming.
By analyzing both of your comments, I figured that the problem has to do with the syntax. I'm pretty lost with pointers and stuff. But the logic of all the rest in the function seems to work as expected.
So, instead of:
char *linkText (constchar *prefix, constchar *sulfix) {
char* textResult=newchar[charLength(prefix)+charLength(sulfix)+1];
* textResult=0;
(...) //charLength is another function that I've done before which equivals to strlen
And now it's working good.
@guestgulkan, thanks for clarifying. I didn't know that such problem was creating memory leaks though.