Home > Back-end >  People who have time to see! C primer plus about new and delete what's the problem
People who have time to see! C primer plus about new and delete what's the problem

Time:09-19

Always prompt: Thread 1: signal SIGABRT
C + + 12.2 (3458, 0 x1000d9dc0) malloc: * * * error for object 0 x100611dec: pointer being freed the was not allocated
C + + 12.2 (3458, 0 x1000d9dc0) malloc: * * * set a breakpoint in malloc_error_break to debug
(LLDB)

//the header file
# ifndef C__12_hpp
# define C__12_hpp

#include
Using STD: : ostream;
Using STD: : cost;
The class String
{
Private:
Char * STR;
int len;
Static int num_strings;
The static const int CINLIM=80;
Public:

String (const char * s);
String ();
String (const String & amp;);
~ the String ();
String & amp; Operator=(const String & amp;);
String & amp; Operator=(const char *);



Void stringlow ();
Void stringup ();
Int has (char c);
Friend String operator + (const String & amp; S, const String & amp; t);
Boolean operator==(const String & amp; S) const;
Friend STD: : ostream & amp; Operator<(STD: : ostream & amp; OS, const String & amp; S);
Friend STD: : cost & amp; Operator> (STD: : cost & amp; Is the String & amp; S);
};

# endif C__12_hpp/* */

//define
# include "c + + 12. HPP
"#include
Int the String: : num_strings=0;

String: : String (const char * s)
{
Len=strlen (s);
STR=new char [len + 1];
Strcpy (STR, s);
+ + num_strings;

}
String: : String ()//modify
{
Len=0;
STR=nullptr;
Num_strings + +;
}
String: : String (const String & amp; S)
{
+ + num_strings;
Len=s.l en;
STR=new char [len + 1];
Strcpy (STR, s.s tr);
}
String: : ~ String ()
{
- num_strings;
The delete [] STR;
}
String & amp; String: : operator=(const String & amp; S)
{
If (this==& amp; S)
Return * this;
The delete [] STR;
Len=s.l en;
STR=new char [len + 1];
Strcpy (STR, s.s tr);
Return * this;
}
String & amp; String: : operator=(const char * s)
{
The delete [] STR;
Len=strlen (s);
STR=new char [len + 1];
Strcpy (STR, s);
Return * this;
}
Bool String: : operator==(const String & amp; S) const
{
Return (STRCMP (STR, s.s tr)==0).
}
Ostream & amp; Operator<(ostream & amp; OS, const String & amp; S)
{
OsReturn the OS;
}
Cost & amp; Operator> (cost & amp; Is the String & amp; S)
{

Char \ [String: : CINLIM];
Is the get (temp, String: : CINLIM);
If (is)
S=temp;
While (is& & Is the get ()! )
='\ n'continue;
The return is;
}

Void String: : stringup ()
{
for(int i=0; iSTR [I]=toupper (STR [I]);

}
Void String: : stringlow ()
{
for(int i=0; iSTR [I]=tolower (STR [I]);

}
Int the String: : from the (char c)
{
Int m=0;
While (* str++)
If (* STR==c)
+ + m;
Return m;
}
String operator + (const String & amp; S, const String & amp; T)
{
A String of m;
M.l en=s.l en + t.l en;
Margaret spellings tr=new char [m.l en + 1];
Strcat (Margaret spellings tr, s.s tr);
Strcat (Margaret spellings tr, t.s tr);
Return m;
}

//master file
#include
using namespace std;
# include "c + + 12. HPP
"Int main () {
String s1 (" and I am a student. The c + + ");
String s2="both Please enter your name:";
String s3.
CoutCin> S3.
S2="My name is" + s3.
CoutS2=s1 + s2;
S2. Stringup ();
Cout<" The string: \ n "& lt; <" 'A' characters in it. \ n ";
S1="red";
String RGB [3]={String (s1), String (" green "), String (" blue ")};
Cout<" Enter the name of a primary for mixing hight: ";
String ans.
Bool success=false;
While (cin> Ans)
{
Ans. Stringlow ();
for(int i=0; i<3; I++)
{
If (ans==RGB [I])
{
Cout<" That's right! \n";
Success=true;
break;
}
}
If (success)
break;
The else
Cout<" Try again! \n";
}
Cout<" Bye \ n ";
return 0;
}

CodePudding user response:

Here with the first memset
String operator + (const String & amp; S, const String & amp; T)
{
A String of m;
M.l en=s.l en + t.l en;
Margaret spellings tr=new char [m.l en + 1];

Memset (Margaret spellings tr, 0, m.l en + 1);

Strcat (Margaret spellings tr, s.s tr);
Strcat (Margaret spellings tr, t.s tr);
Return m;
}
Should have any other questions, you'll see

CodePudding user response:

The
reference 1/f, Simple, Soft reply:
add here first memset
String operator + (const String & amp; S, const String & amp; T)
{
A String of m;
M.l en=s.l en + t.l en;
Margaret spellings tr=new char [m.l en + 1];

Memset (Margaret spellings tr, 0, m.l en + 1);

Strcat (Margaret spellings tr, s.s tr);
Strcat (Margaret spellings tr, t.s tr);
Return m;
}
Should have any other questions, you see
tried or so

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related