just overloading the addition operator to allow the concatenation of two strings to make your line of code:
concatstr=s1+s2;
is possible to use without the compiler complaining.
This:
1 2 3 4
strings(char *c)
{
strcpy(s,c);
}
is you class's contructor, initialising the s char array to whatever you are passing in ('test' in one object and 'run\0' in the other in your example).
This code is completely broken. It will not work and potentially with either make your program crash, or will corrupt memory making your program do very strange things.
@ OP: Disch is 100% right, this is broken. It's easy enough to fix but it shows that the source you took this from has no idea what they are doing. The problem is here:
char* temp;
That declares a pointer true enough but it doesn't give it anything to point to. Since this is clearly C, this should read:
I already execute this program. This program working is well.
If you are calling that + operator... then you are corrupting memory. That code is broken. If it happens to work for you, it's a fluke that the memory you are corrupting does not happen to be used by anything else.
char *operator+(strings x1)
{
// This line creates a pointer named 'temp'. A pointer is just an address that "points" to
// some other area in memory. Usually they point to a buffer/array so that memory
// can be accessed freely... however you never initialize 'temp' to point to ANYTHING.
//
// Therefore temp is pointing to random garbage. It might be pointing to unused
// memory. Or it might be pointing to something inaccessible (which would crash the
// program). Or it might be pointing to memory being used by something else (in which
// case variables in your program will magically change values on you even when you
// aren't touching them). There's no way to know what will happen. It's completely
// random.
char *temp;
// This next line attempts to copy all the characters in the buffer pointed to by 's' to the
// buffer pointed to by 'temp'. But again.. temp does not point to a buffer, so this
// copies the 's' characters to some random address in memory... completely destroying
// whatever information was at that address
strcpy(temp,s);
// This does a similar thing... only instead of copying the string data to 'temp', it appends
// it to the end of the string data stored at 'temp'. But again.. temp points to garbage
// so this is more memory corruption. Bad bad bad bad bad.
strcat(temp,x1.s);
// This returns temp as the function output. This is the address to which we've copied our
// string data. However it's some random garbage value, so the string data might not
// be there anymore. Some other part of the program may have wrote over it... or something
// else could have happened to it.
return temp;
EDIT:
Computergeek wrote:
That declares a pointer true enough but it doesn't give it anything to point to. Since this is clearly C, this should read:
It's not C if he's overloading the + operator. It has to be C++.
And yes... using malloc to allocate a buffer will solve the heap corruption problem, but will introduce memory leaks.
Line 3: This is an uninitialized pointer. The value of the pointer is garbage.
Line 4: Copies member C-string s to the memory pointed at by garbage pointer temp. This could be anywhere in memory and will cause a trap if outside the bounds of your program.
Line 5: Concatenates C-string s from instance x1 to temp (which is still a garbage pointer). Again, you're writing over who knows what.
Line 6: You're returning the garbage pointer.
edit Ninja'd x4
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
@ Disch: Good catch, I somehow forgot that C doesn't provide overloading, time for more coffee. Memory leaks are a design issue to be addressed later, I thought to establish the basics of not running over your memory addresses now then introduce free() my next post.