Connecting Tech Pros Worldwide Forums | Help | Site Map

Messed Up Char Array

A_StClaire_@hotmail.com
Guest
 
Posts: n/a
#1: Aug 29 '05

hi,

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.

thx a bunch


#include <iostream>
using namespace std;

class Contact_Info {
static const int max_size = 28;
char name[max_size];
int phone_number;
public:
Contact_Info() {
char ee[] = "Empty entry.";
int x = 0;

while(ee[x]) {
name[x] = ee[x++];
}

phone_number = NULL;
}
Contact_Info(char* ptr) {
int x = 0;

while(name[x]) {
name[x++] = NULL;
}

x = 0;

while(*ptr) {
name[x++] = *ptr++;
}

phone_number = NULL;
}
Contact_Info(char* ptr, int a) {
int x = 0;

while(name[x]) {
name[x++] = NULL;
}

x = 0;

while(*ptr) {
name[x++] = *ptr++;
}

phone_number = a;
}
void display_all() {
int x = 0;
cout << "Name: ";

while(name[x]) {
cout << name[x++];
}

cout << "\n\nPhone number: " << phone_number <<
"\n\n\n\n";
}
};

int main() {
Contact_Info Sample("John Smith", 1234567);

auto_ptr<Contact_Info> ap(&Sample);
auto_ptr<Contact_Info> ap2(new Contact_Info());

ap->display_all();

ap2->display_all();

ap2 = ap;

//ap->display_all(); //This line causes program crash (ap now
points to nothing).

ap2->display_all();

cin.get();
}


David Hilsee
Guest
 
Posts: n/a
#2: Aug 29 '05

re: Messed Up Char Array


<A_StClaire_@hotmail.com> wrote in message
news:1125275195.254354.3620@z14g2000cwz.googlegrou ps.com...
[...][color=blue]
> class Contact_Info {
> static const int max_size = 28;
> char name[max_size];
> int phone_number;[/color]
[...][color=blue]
> Contact_Info(char* ptr, int a) {
> int x = 0;
>
> while(name[x]) {
> name[x++] = NULL;
> }
>[/color]
[...]

The contents of the array (name) are uninitialized. You could easily walk
off the end of the array if you look for a '\0' to stop the loop. That
could explain the strange behavior you're seeing.

Change the while loop to something like

for ( x = 0; x < max_size; ++x ) {
name[x] = '\0';
}

--
David Hilsee


A_StClaire_@hotmail.com
Guest
 
Posts: n/a
#3: Aug 29 '05

re: Messed Up Char Array



appreciate it, David. thx.


David Hilsee wrote:[color=blue]
> <A_StClaire_@hotmail.com> wrote in message
> news:1125275195.254354.3620@z14g2000cwz.googlegrou ps.com...
> [...][color=green]
> > class Contact_Info {
> > static const int max_size = 28;
> > char name[max_size];
> > int phone_number;[/color]
> [...][color=green]
> > Contact_Info(char* ptr, int a) {
> > int x = 0;
> >
> > while(name[x]) {
> > name[x++] = NULL;
> > }
> >[/color]
> [...]
>
> The contents of the array (name) are uninitialized. You could easily walk
> off the end of the array if you look for a '\0' to stop the loop. That
> could explain the strange behavior you're seeing.
>
> Change the while loop to something like
>
> for ( x = 0; x < max_size; ++x ) {
> name[x] = '\0';
> }
>
> --
> David Hilsee[/color]

Old Wolf
Guest
 
Posts: n/a
#4: Aug 29 '05

re: Messed Up Char Array


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.

Jay Nabonne
Guest
 
Posts: n/a
#5: Aug 29 '05

re: Messed Up Char Array


On Sun, 28 Aug 2005 17:26:35 -0700, A_StClaire_ wrote:
[color=blue]
> name[x] = ee[x++];[/color]

In addition to the other comments, I would not do the above. I don't think
you have any guarantees about what "x" will be when name[x] is evaluated.

- Jay
red floyd
Guest
 
Posts: n/a
#6: Aug 29 '05

re: Messed Up Char Array


A_StClaire_@hotmail.com wrote:[color=blue]
> hi,
>
> wrote the following program to learn a bit about auto pointers.
> [redacted][/color]
[color=blue]
>
> int main() {
> Contact_Info Sample("John Smith", 1234567);
>
> auto_ptr<Contact_Info> ap(&Sample);[/color]

NO NO NO NO NO NO NO NO NO!!!
DO NOT DO THIS!!! EVER!!!

auto_ptr<> will delete its pointer when it goes out of scope or is
reassigned. Sample was not dynamically allocated, so somewhere down the
line, you will attempt to delete a non-dynamically allocated variable.

[color=blue]
> auto_ptr<Contact_Info> ap2(new Contact_Info());
>
> ap->display_all();
>
> ap2->display_all();
>
> ap2 = ap;
>
> //ap->display_all(); //This line causes program crash (ap now
> points to nothing).
>
> ap2->display_all();
>
> cin.get();
> }
>[/color]
A_StClaire_@hotmail.com
Guest
 
Posts: n/a
#7: Aug 31 '05

re: Messed Up Char Array



thx for all the helpful admonitions, guys.


red floyd wrote:[color=blue]
> A_StClaire_@hotmail.com wrote:[color=green]
> > hi,
> >
> > wrote the following program to learn a bit about auto pointers.
> > [redacted][/color]
>[color=green]
> >
> > int main() {
> > Contact_Info Sample("John Smith", 1234567);
> >
> > auto_ptr<Contact_Info> ap(&Sample);[/color]
>
> NO NO NO NO NO NO NO NO NO!!!
> DO NOT DO THIS!!! EVER!!!
>
> auto_ptr<> will delete its pointer when it goes out of scope or is
> reassigned. Sample was not dynamically allocated, so somewhere down the
> line, you will attempt to delete a non-dynamically allocated variable.
>
>[color=green]
> > auto_ptr<Contact_Info> ap2(new Contact_Info());
> >
> > ap->display_all();
> >
> > ap2->display_all();
> >
> > ap2 = ap;
> >
> > //ap->display_all(); //This line causes program crash (ap now
> > points to nothing).
> >
> > ap2->display_all();
> >
> > cin.get();
> > }
> >[/color][/color]

Closed Thread