Question: I have a class that contains an attribute char \* variable name that points to an allocated char array from new char[] that contains a C style text string. I'm using overloaded constructor that has attribute char[] variable name\_val and is equal to "emtpy".In member initialization list, i have initialized private class member name to nullptr and, inside of a constructor, I have allocated space for it depending on size of name\_val, copied it to name using strcpy. When I compile program i get warning that says,
[Warning] deprecated conversion from string constant to 'char\*' [-Wwrite-strings].I am also seeing a memory leak. So i spent little time messing around and as soon as I set the char name\_val to const char name\_val[]="empty", inside overloaded constructor everything works perfectly fine.

Here is the code, and note that,inside of overloaded constructor the attribute char[] variable name name\_ val is not set as const!

Can someone please explain, what is the purpose of const in this case?

```
#include<iostream>
#include<cstring>

using namespace std;

class Person{
    char *name;
public:
    Person(char name_val[]="empty"):
        name{nullptr}{
            name=new char[strlen(name_val)+1];
            strcpy(name,name_val);
        }
    ~Person(){
        delete [] name;name=nullptr;
    }

    char *get_Name()const{return name;}
};

int main(){

    Person p;
    cout<<p.get_Name()<<endl;
    return 0;
}

```

Answer: 
> 
> Initialization class member using initializer list results a memory leak
> 
> 
> I am also seeing a memory leak.
> 
> 
> 

There is no memory leak in the shown program. It is unclear why you would think that you see it.

> 
> Can someone please explain, what is the purpose of const in this case?
> 
> 
> 

Object being const means that its state (except for mutable members in case of class types) cannot be modified. Pointer / reference to const means that the pointed / referred object may not be modified through the pointer / reference.

A pointer to non-const can implicitly be converted to pointer to const, but the conversion back is not implicit.

String literals are const. A pointer to non-const (`char*`) cannot be assigned to point to a const object since C++11. The warning message that you see implies that you're using a pre C++11 version of C++. Back then conversion from string literal to `char*` was well-formed, but the conversion was deprecated.

---

P.S. `Person` has a serious design flaw: The class is copyable, but if you ever make a copy intentionally, or incidentally, the behaviour of the program will be undefined.

P.P.S. The default member initialiser is redundant, since you never use it.
When you write:

```
Person(char name_val[]="empty")

```

it is equivalent to writing:

```
Person(char *name_val="empty")

```

and now you can see that you are assigning a string constant to non-const `char *`, hence the warning. The solution (obviously) is to write:

```
Person(const char *name_val="empty")

```

and then the compiler will be happy.

OTOH, when you write:

```
Person()
{
    char name_val[]="empty"
    ...
}

```

then you are creating a character array on the stack and copying the given string constant into it, so no `const` is required (although it would still be a good idea in this instance).

I don't know why you think you have a memory leak. I don't see one in your code.