Is it memory leak?

Hi everyone.

If I use a new operator without delete or I assign a new data to pointer with a new opreator I do a memory leak.

But piece of code below can it make memory leak?

1
2
3
  char *a = "Some text";
  char *b = "Another text";
  a = b; // this line can it a make memory leak or something dangerous? 


Sorry for my English.
It's not a memory leak (ther was nothing new'd), but it is something dangerous: your pointers are pointing at read-only memory, but their types are "pointer to modifiable char". It's better (and, since C++11, it's the only valid code) if their types are "pointers to const char", as in
1
2
3
const char *a = "Some text";
const char *b = "Another text";
a = b;

Last edited on
You have right. Thanks

I tested this:

1
2
3
char *a = "Some text";
char *b = "Another text";
b[2] = 'c';


Program make a crash.
String literals have static storage duration. That is the storage for string luterals is not allocated dynamically.
Topic archived. No new replies allowed.