CRT detected that the application wrote to memory after end of heap buffer

    最近闲的无聊,把C++ STL中的string容器类自己尝试实现一遍,但是在写到insert()函数时,出现一个错误,就是CRT deceted that the application wrote to memory after end of heap buffer,最后上网看了些博客,自己调了下代码,解决了该问题。

    一般的原因是对内存块进行不当的操作,比如字符串最后不加NULL或者'\0',导致程序释放内存时超出编辑;或者是写数据时超出申请的空间大小。

代码如下:

MyString& MyString::insert(unsigned int ipos,const char *stem)            //任意位置插入函数
{
    MyString newString;
    int iIndex;
    if (ipos >= 0 && ipos < strlen(m_data))                               //ipos在范围内部
    {
        newString.m_data = new char[strlen(m_data) + strlen(stem) +1];     //申请空间
        newString.m_end = m_data + strlen(m_data) + strlen(stem);
        for (iIndex = 0;iIndex < strlen(m_data) + strlen(stem) -1;++iIndex)
        {
            if (iIndex < ipos)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值