0

This is my code. I create a char array and assign a string literal to it by operator=. After which, I free it by delete.However, it cause segmentation. But it works fine with strcpy. Besides, Is char array always assigned by strcpy?

I got to this problem from Implementaion of STL std::string, I wonder why private char* _data is always assigned by strcpy, can't it assign by operator=? since char* A= "HELLO WORLD" works as well.

int main()
{
    char* a=new char[3];
    a="12";
    //strcpy(a,"aa");
    delete[] a;

}

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Caiyi Zhou
  • 143
  • 1
  • 9
  • `a` is a pointer, and `a="12";` sets it to point at a string literal instead of whatever it pointed at before. It does not copy the string into the array you allocated, it just changes the pointer to point at the literal instead. – Dmitri Feb 20 '21 at 07:48
  • If you're trying to make your own `std::string` alternative you'll need a proper `class` and then use `operator=` with an override. – tadman Feb 20 '21 at 07:50

2 Answers2

1

You can only delete[] something that you allocate with new[].

So this code is OK, because a points to memory you allocated with new[]

int main()
{
    char* a = new char[3];
    delete[] a;
}

This code is also OK, because a is still pointing at memory allocated with new[]

int main()
{
    char* a=new char[3];
    strcpy(a, "aa"); // this doesn't change a
    delete[] a;
}

But this code is different. In this code a starts pointing at memory allocated with new[] but then you change the pointer. You make a point at "aa". That is not memory allocated with new[] and so you get a crash.

int main()
{
    char* a=new char[3];
    a = "aa"; // this does change a
    delete[] a;
}

The difference between the second and third version, is that the second version changes the characters that a is pointing at, but the third version changes the pointer a itself. That's the crucial difference.

When you work with pointers you must understand the difference between changing the pointer and changing whatever the pointer is pointing at. These are two different things.

john
  • 85,011
  • 4
  • 57
  • 81
0

You dynamically allocated a character array

char* a=new char[3];

After this statement the pointer a points to the dynamically allocated memory.

But then you reassigned the pointer

a="12";

So the address of the allocated memory was lost. And the code produces a memory leak. Now the pointer points to the first character of the string literal "12" that has static storage duration. And you are trying to free this memory with the static duration that was not allocated dynamically.

delete[] a;

So the error occurs.

You should use for example the standard string function strcpy to copy the string literal in the allocated extent of memory.

strcpy( a, "12" );

The difference between these two statements

a = "12";

and

strcpy( a, "12" );

is that in the first statement you are changing the value stored in the pointer a. That is after such a statement the pointer points to another extent of memory with another address.

In the second statement the pointer itself is not being changed. It is the extent of the memory pointed to by the pointer that is being changed. The pointer keeps its value without changing it.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335