A_StClaire_@hotmail.com wrote:[color=blue]
>
> wrote the following program to learn a bit about auto pointers.
> weird thing is, the char array 'name' with size 'max_size'
> displays a bunch of messed up symbols after the "John Smith"
> if max_size is larger than 28. why is that? I found this out
> by playing with max_size after I couldn't figure out what I
> did wrong.
>
> class Contact_Info {
> static const int max_size = 28;
> char name[max_size];[/color]
At this point name[] looks like: [???????????????????????????]
ie. it is initialized to garbage. (I use sq brackets instead
of quote marks to emphasize that it is an array, not a string).
[color=blue]
> int phone_number;
> public:
> Contact_Info() {
> char ee[] = "Empty entry.";
> int x = 0;
>
> while(ee[x]) {
> name[x] = ee[x++];
> }[/color]
Now name looks like: [Empty entry.????????????????]
Note that there is no end-of-string marker in this.
[color=blue]
>
> phone_number = NULL;[/color]
NULL should only be used for pointers. Use a plain 0 for
numbers.
[color=blue]
> }
> Contact_Info(char* ptr) {
> int x = 0;
>
> while(name[x]) {
> name[x++] = NULL;
> }[/color]
Are you sure you know what " while(name[x]) " does ? It will keep
going as long as the x'th char in the name array is not an
end-of-string marker. If you run past the end of the array, this
loop doesn't care, it will keep on reading whatever happened to
be in memory after the array.
Since name[] currently contains garbage (see above), it is just
a matter of chance as to whether this code writes all over your
memory space, or it encounters an end-of-string marker and stops.
My advice would be to get rid of that loop entirely, since you
are just about to assign values to name[] in the next section!
[color=blue]
>
> x = 0;
>
> while(*ptr) {
> name[x++] = *ptr++;
> }[/color]
Now name will be [John Smith??????????????????]. Note that there
is still no end-of-string marker in Name.
[color=blue]
>
> phone_number = NULL;
> }
> void display_all() {
> int x = 0;
> cout << "Name: ";
>
> while(name[x]) {
> cout << name[x++];
> }[/color]
This will simply keep displaying characters from name[] until
it runs into an end-of-string marker. Above, we worked out what
name[] contains. So now it should be no surprise that it displays
the string, and then some quantity (possibly endless) of garbage.
Now we move onto the auto pointers :)
[color=blue]
> int main() {
> Contact_Info Sample("John Smith", 1234567);
>
> auto_ptr<Contact_Info> ap(&Sample);[/color]
A bad idea. Since 'Sample' is an automatic object, it will
automatically be deleted when it goes out of scope. Your code
will try to delete it twice (causing undefined behaviour).
[color=blue]
> auto_ptr<Contact_Info> ap2(new Contact_Info());[/color]
Better :)
Note that those extra brackets are a waste of space:
auto_ptr<Contact_Info> ap2(new Contact_Info);[color=blue]
>
> ap->display_all();
> ap2->display_all();
> ap2 = ap;[/color]
This statement will free ap2 (I think!), and transfer ownership
from ap to ap2. Now ap points to nothing, and ap2 points to
Sample.
[color=blue]
> //ap->display_all(); //This line causes program crash
> (ap now points to nothing).[/color]
Correct.
[color=blue]
> ap2->display_all();
>
> cin.get();
> }[/color]
As mentioned before, now you may also get a crash, as Sample gets
deleted twice.