Pointer Questions

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;
....
.....
 
I

Ian Collins

Vishesh said:
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 ?
Nothing, it is a string literal, the life of string literals is the
lifetime of the program.
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 ?
No, you are assigning a value to the pointer, not changing anything it
may be pointing to.
Another thing I have noticed is that supose u have a class or a
structure TClass (exmple name take any u want).
u?

TClass *a;
a[0] = SomeValue;
a[1] = SomeOtherValue;

This works but again isn't it unsafe shouldn't it be something like..

It is undefined, so it may "work" or your toilet might explode.
TClass *a = new TClass[size];
a[0] = SomeValue;
a[1]= SomeOtherVaue;

Yes.
 
R

Rolf Magnus

Vishesh said:
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 ?

For the program, there is no difference. If it doesn't have any pointer to
it, it can't be accessed.
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 ?

String literals are wherever the one who made the compiler sees fit. There
is no special rule. Trying to overwrite them results therefore in undefined
behavior.
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

It might appear to work, but again, the behavior of the above code is not
just unsafe, but undefined. The pointer is uninitialized, so it points to
some random location in memory. You must not dereference it until properly
initialized.
shouldn't it be something like..
TClass *a = new TClass[size];
a[0] = SomeValue;
a[1]= SomeOtherVaue;
...
....

Yes.
 
V

Vishesh

Alright then. Thanks for the replies

These 2 aspects had been bugging me for some while now !!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top