Is it memory leak?

May 17, 2013 at 9:32pm
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.
May 17, 2013 at 9:46pm
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 May 17, 2013 at 9:46pm
May 18, 2013 at 3:43pm
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.
May 18, 2013 at 4:02pm
String literals have static storage duration. That is the storage for string luterals is not allocated dynamically.
Topic archived. No new replies allowed.