Char pointers overload

Hello, forum.

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:

The function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char *linkText (const char *prefix, const char *sulfix) {
	char *textResult=new char;
	const char *textArray[2]={ prefix, sulfix };

	int textOffset[2]={0,0};

	for(int i=0; i<2; i++) {
		while( true ) {
			if( textArray[i][textOffset[i]] != '\0' ) {
				textResult[textOffset[0]+textOffset[1]] = textArray[i][textOffset[i]];
			}
			else break;
			textOffset[i]++;
		}
	}

	textResult[textOffset[0]+textOffset[1]]='\0';

    return textResult;
}


In the int main()
1
2
3
4
5
6
	char *myString = " Love ";

	for(int i=1; i<=10; i++) {
		myString = linkText( myString, "o " );
        cout << myString << endl;
	}


Output:
 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?

Thanks!
Last edited on
This seems to be a real convoluted (and flawed) way of joining two string together.

Here are some wrong things:

1. char *textResult=new char;
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 (const char *prefix, const char *sulfix) returns a pointer
to the memory allocated by new .
This memory is never deleted causing memory leaks.
Last edited on
Because I feill like showing off, here's the same function, but well-written:
1
2
3
4
5
6
7
8
9
char *linkText(const char *s1,const char *s2){
	size_t l1=strlen(s1),
		l2=strlen(s2);
	char *res=new char[l1+l2+1];
	*res=0;
	strcat(res,s1);
	strcat(res,s2);
	return res;
}
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:
1
2
3
char *linkText (const char *prefix, const char *sulfix) {
	char *textResult=new char;
	(...)

I put:
1
2
3
4
char *linkText (const char *prefix, const char *sulfix) {
	char* textResult=new char[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.

Some test:
1
2
3
4
5
6
    char *testString="Hello,";

    for(int i=0; i<25; i++) {
        testString=linkText(testString, linkText(" ",intToChar(i+1)));
    }
    cout << testString << endl;

Output:
Hello, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 (...) 25


Thanks! :)
Last edited on
Topic archived. No new replies allowed.