0

Why can not I perform the following line?

delete [] target;

in my case?

Here is the code:

main.cpp

#include <iostream>
using namespace std;
#include "Char.h"

int main()
{
    char * text = "qwerty";
    char * a = new char[charlen(text)+1];
    copyon(a,text);
    cout<<a<<endl;

    char * test = "asdfghjkl";
    assign(&a, test);
    assign(&a, a);

    char * test1 = new char[26];
    for (int i(0); i < 26; i++)
    {
        test1[i] = char(i+65);
    }
    test1[26] = '\0';
    anotherAssign(test1, a);

    cout << test1 << endl;

    return 0;
}

Char.cpp

#include "Char.h"
#include <iostream>
#include <cassert>
#include <cstring>

size_t charlen(const char * ps)
{
    size_t len=0;
    while (ps[len++]);
    assert(len-1==strlen(ps));
    return len;
}
void assign(char** target, const char* source)
{
    if (*target==source)
        return;
    delete [] *target;
    *target = new char[charlen(source)+1];
    copyon(*target, source);
    return;
}

void anotherAssign(char* target, const char* source)
{
    if (target==source)
        return;
    delete [] target;
    target = new char[charlen(source)+1];
    copyon(target, source);
    return;
}

void copyon(char* const target, const char* const source)
{
    char * t = target;
    const char * s = source;
    while (*t++ = *s++);
    //while(*target++ = *source++)
    //  ;
    std::cout << target << " source = " << source << std::endl;
    return;
    size_t len = charlen(source);
    //for (size_t i=0; i<len; ++i)
    //  target[i]=source[i];
    //target[len]='\0';
}

Here is an exception:

enter image description here enter image description here

5
  • 1) "And what is the difference between using: <...>" Do you understand the difference between pass-by-value, and pass-by-reference? 2) Did you try to step-through your code with a debugger while inspect the values in variables? Commented Nov 9, 2017 at 11:52
  • Yes. I tried to do that. My question is not about the difference you are talking about, but about the difference in my case. Commented Nov 9, 2017 at 11:53
  • "My question is not about the difference you are talking about, but about the difference in my case." The difference is exactly the same. Commented Nov 9, 2017 at 11:54
  • @AlgirdasPreidžius, got it, Thank you very much. Commented Nov 9, 2017 at 11:55
  • The statement test1[26] = '\0'; gives undefined behaviour, since test1 has been assigned to new char[26]. Once an instance of undefined behaviour has occurred, any subsequently executed code can also potentially behave incorrectly whether it has flaws or not - in your case, including the call of anotherAssign() or any code it executes. Commented Nov 9, 2017 at 11:56

1 Answer 1

3

If you do:

char * test1 = new char[26];

then your array will go from test1[0] to test1[25].

That means:

test1[26] = '\0';

is out of bounds. At this point, the head is corrupted, and what happens next is undefined (but rarely desirable).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.