分配内存错误
来源:互联网 发布:淘宝卖家app叫什么 编辑:程序博客网 时间:2024/06/05 23:46
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <malloc.h>
using namespace std;
class myString{
public:
char* pStr;
myString(const char* p)
{
pStr = new char[20];//此处不能使用(),使用()只会创建一个空间
pStr = (char*)malloc(20);
strcpy(pStr,p);
}
myString& operator+(const myString& rhs)
{
cout<<strlen(rhs.pStr)<<"\t"<<strlen(pStr)<<endl;
char * temp = new char(41);
strcpy(temp,pStr);
strcat(temp,rhs.pStr);
delete pStr;
pStr = temp;
cout <<pStr<<endl;
return *this;
}
};
int main ()
{
char str1[]={"Sampletring1"};
char str2[40];
char str3[]={"Sampletring2"};
myString pA(str1);
myString pB(str1);
myString pC(str3);
cout<<pA.pStr<<endl;
cout<<pB.pStr<<endl;
cout<<pC.pStr<<endl;
//pA +pA;
return 0;
}
#include <string.h>
#include <iostream>
#include <malloc.h>
using namespace std;
class myString{
public:
char* pStr;
myString(const char* p)
{
pStr = new char[20];//此处不能使用(),使用()只会创建一个空间
pStr = (char*)malloc(20);
strcpy(pStr,p);
}
myString& operator+(const myString& rhs)
{
cout<<strlen(rhs.pStr)<<"\t"<<strlen(pStr)<<endl;
char * temp = new char(41);
strcpy(temp,pStr);
strcat(temp,rhs.pStr);
delete pStr;
pStr = temp;
cout <<pStr<<endl;
return *this;
}
};
int main ()
{
char str1[]={"Sampletring1"};
char str2[40];
char str3[]={"Sampletring2"};
myString pA(str1);
myString pB(str1);
myString pC(str3);
cout<<pA.pStr<<endl;
cout<<pB.pStr<<endl;
cout<<pC.pStr<<endl;
//pA +pA;
return 0;
}
- 分配内存错误
- 内存分配失败错误处理
- 内存分配失败错误处理
- 内存分配错误例子& 改正
- 内存分配失败错误处理
- 内存分配笔记-内存分配概念和常见错误
- 内存分配访问无效。 998 错误提示
- C++内存分配方式及常见错误
- C++内存分配方式及常见错误
- 内存分配方式及常见错误
- 内存分配方式及常见错误
- C关于内存分配的错误解析
- 内存分配方式及常见错误
- 内存分配方式及常见错误
- 关于分配内存或释放内存的错误
- 内存出错,内存泄露,数组越界,悬空指针,错误分配
- 常见的内存的分配错误及其对策
- 【日常小记】内存分配方式及常见错误
- Mysql安装
- 摘自代码阅读方法与实践书籍的知识点总结
- ZFI001应收账龄
- 全文索引如果关键字少于3个字符就会查不到
- Eclipse快捷键大全
- 分配内存错误
- linux源代码阅读技巧
- Java GUI
- 黑马程序员_第一篇文章
- 开源.net程序反编译工具 - ILSpy
- 使用服务器变量过滤XSLT Webpart数据
- MFC 消息处理顺序
- 石家庄铁道学院ARCGIS培训
- 个人安卓学习笔记---第一个Android应用程序