V
Vishesh
I have often seen in various sourcecodes that isntead a normal char
array they use a pointer to a char.
Like Instead of
char test[] = "C++";
char * test2 = "C++";
I realize the difference that the latter points to a const string
literal. Now if I change the value of test2 like so-
test2 = "Blah";
test2 now points to a new const string literal with the above value.
But what happens to the earlier one ? Is it destroyed or just left
there or what ?
And isn't this way of declaring a string unsafe. I mean worst case
scenario this pointer points to a memory location where some data of
yours is stored. Won't it overwrite that date with the string ? Or is
thier a seperate heap for string literals ?
Another thing I have noticed is that supose u have a class or a
structure TClass (exmple name take any u want).
TClass *a;
a[0] = SomeValue;
a[1] = SomeOtherValue;
This works but again isn't it unsafe shouldn't it be something like..
TClass *a = new TClass[size];
a[0] = SomeValue;
a[1]= SomeOtherVaue;
....
.....
array they use a pointer to a char.
Like Instead of
char test[] = "C++";
char * test2 = "C++";
I realize the difference that the latter points to a const string
literal. Now if I change the value of test2 like so-
test2 = "Blah";
test2 now points to a new const string literal with the above value.
But what happens to the earlier one ? Is it destroyed or just left
there or what ?
And isn't this way of declaring a string unsafe. I mean worst case
scenario this pointer points to a memory location where some data of
yours is stored. Won't it overwrite that date with the string ? Or is
thier a seperate heap for string literals ?
Another thing I have noticed is that supose u have a class or a
structure TClass (exmple name take any u want).
TClass *a;
a[0] = SomeValue;
a[1] = SomeOtherValue;
This works but again isn't it unsafe shouldn't it be something like..
TClass *a = new TClass[size];
a[0] = SomeValue;
a[1]= SomeOtherVaue;
....
.....