Pls explain this program.. How will executed??

Aug 29, 2014 at 1:37pm
Write your question here.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class strings
{
char s[20];
public:
strings()
{
s[0]='\0';
}
strings(char *c)
{
strcpy(s,c);
} char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
};
void main()
{
clrscr();
strings s1("test"), s2("run\0");
char *concatstr;
concatstr=s1+s2;
cout<<"\n Concatenated string"<<concatstr;
getch();
}


Can any one explain this step only. How will working this function.

strings(char *c)
{
strcpy(s,c);
}

char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
}
Aug 29, 2014 at 1:58pm
http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

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).

You wouldn't need to do this nowadays. you would not use char arrays, you would use std::string which has already overloaded the addition operator for you:
http://www.cplusplus.com/reference/string/string/operator+/
Last edited on Aug 29, 2014 at 2:00pm
Aug 29, 2014 at 2:00pm

Dear mutexe

Please explain this step only
char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;
Aug 29, 2014 at 2:03pm
1
2
3
4
5
6
char *operator+(strings x1)
{
char *temp;
strcpy(temp,s);
strcat(temp,x1.s);
return temp;



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.
Aug 29, 2014 at 2:04pm
read that first link on operator overloading.
then the other stuff is just normal C.
http://www.cplusplus.com/reference/cstring/strcpy/
http://www.cplusplus.com/reference/cstring/strcat/

Aug 29, 2014 at 2:05pm
Disch

I already execute this program. This program working is well. But I cannot understand this above step only.. Kindly How this working this code...
Aug 29, 2014 at 2:10pm
@ 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:
 
char* temp = (char*) malloc(strlen(x1) + 1); //Don't forget the NULL terminator 
Aug 29, 2014 at 2:11pm
Like disch said it's a bit broke

even this:
1
2
3
#include<iostream.h>
#include<conio.h>
#include<string.h> 

you're using deprecated stuff.

strcpy itself is unsafe.
and you aren't initialising/assigning/returning pointers like you should.

As i said, i'd use strings if i was you.

Aug 29, 2014 at 2:15pm
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.

I can guarantee the code is broken.


Kindly How this working this code...


Again... it doesn't work. It's broken.

But okay. Here is what it's actually doing:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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.
Last edited on Aug 29, 2014 at 2:18pm
Aug 29, 2014 at 2:17pm
It looks like Turbo C++ (an ancient dialect which is not really C++).
As I stated previously,
First use strlen() to determine the sizes of s and x1.s. Add those together plus 1 for null terminator.

http://www.cplusplus.com/forum/beginner/140681/#msg743214
Aug 29, 2014 at 2:20pm
I already execute this program

That doesn't mean it's right.

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.

Last edited on Aug 29, 2014 at 2:22pm
Aug 29, 2014 at 2:20pm
Thanks Disch
Aug 29, 2014 at 2:55pm
@ 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.
Topic archived. No new replies allowed.