473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Messed Up Char Array


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();
}

Aug 29 '05 #1
6 2293
<A_*********@hotmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
[...]
class Contact_Info {
static const int max_size = 28;
char name[max_size];
int phone_number; [...] Contact_Info(char* ptr, int a) {
int x = 0;

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

[...]

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
Aug 29 '05 #2

appreciate it, David. thx.
David Hilsee wrote:
<A_*********@hotmail.com> wrote in message
news:11********************@z14g2000cwz.googlegrou ps.com...
[...]
class Contact_Info {
static const int max_size = 28;
char name[max_size];
int phone_number;

[...]
Contact_Info(char* ptr, int a) {
int x = 0;

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

[...]

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


Aug 29 '05 #3
A_*********@hotmail.com wrote:

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];
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).
int phone_number;
public:
Contact_Info() {
char ee[] = "Empty entry.";
int x = 0;

while(ee[x]) {
name[x] = ee[x++];
}
Now name looks like: [Empty entry.????????????????]
Note that there is no end-of-string marker in this.

phone_number = NULL;
NULL should only be used for pointers. Use a plain 0 for
numbers.
}
Contact_Info(char* ptr) {
int x = 0;

while(name[x]) {
name[x++] = NULL;
}
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!

x = 0;

while(*ptr) {
name[x++] = *ptr++;
}
Now name will be [John Smith??????????????????]. Note that there
is still no end-of-string marker in Name.

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

while(name[x]) {
cout << name[x++];
}
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 :)
int main() {
Contact_Info Sample("John Smith", 1234567);

auto_ptr<Contact_Info> ap(&Sample);
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).
auto_ptr<Contact_Info> ap2(new Contact_Info());
Better :)
Note that those extra brackets are a waste of space:
auto_ptr<Contact_Info> ap2(new Contact_Info);
ap->display_all();
ap2->display_all();
ap2 = ap;
This statement will free ap2 (I think!), and transfer ownership
from ap to ap2. Now ap points to nothing, and ap2 points to
Sample.
//ap->display_all(); //This line causes program crash
(ap now points to nothing).
Correct.
ap2->display_all();

cin.get();
}


As mentioned before, now you may also get a crash, as Sample gets
deleted twice.

Aug 29 '05 #4
On Sun, 28 Aug 2005 17:26:35 -0700, A_StClaire_ wrote:
name[x] = ee[x++];


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
Aug 29 '05 #5
A_*********@hotmail.com wrote:
hi,

wrote the following program to learn a bit about auto pointers.
[redacted]
int main() {
Contact_Info Sample("John Smith", 1234567);

auto_ptr<Contact_Info> ap(&Sample);
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.

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();
}

Aug 29 '05 #6

thx for all the helpful admonitions, guys.
red floyd wrote:
A_*********@hotmail.com wrote:
hi,

wrote the following program to learn a bit about auto pointers.
[redacted]


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

auto_ptr<Contact_Info> ap(&Sample);


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.

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();
}


Aug 31 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
21
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
13
by: chellappa | last post by:
hi , please explain me , char pointer , char Array, char ,string... i have some many confussion about this... please clarify to me.. i need some example about this program by chellappa.ns
15
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.