problem with array of struct

M

Markus

Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

..
..

int main void() {
t_datensatz ds[2];

ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;

// This doesn't work, why??
for(int i=0;i<=2;i++)
cout << "OUT: " << ds.flag << ds.ID << " " <<
ds.Tel <<
endl;

}

Produced Output:
OUT: 12TelegrammmmmmmmmmmmmmmmmmmmmA
OUT: 34TelegrammmmmmmmmmmmmmmmmmmmmB
OUT: 56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 12 TelegrammmmmmmmmmmmmmmmmmmmmA34TelegrammmmmmmmmmmmmmmmmmmmmB56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 34 TelegrammmmmmmmmmmmmmmmmmmmmB56TelegrammmmmmmmmmmmmmmmmmmmmC
OUT: 56 TelegrammmmmmmmmmmmmmmmmmmmmC

Ok, the first three lines are fine. But why does the for-statement not
work as it is intented to??? It should produce exactly the same output
as the "manual" cout-statements.
I guess the problem is "ds.Tel". What's wrong in that little
program?

Tnx,
Markus
 
N

Noah Roberts

Markus said:
Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

.
.

int main void() {
t_datensatz ds[2];

Holds 2 items.
ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

The following results in overflow of ds.
ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;

// This doesn't work, why??
for(int i=0;i<=2;i++)
cout << "OUT: " << ds.flag << ds.ID << " " <<
ds.Tel <<
endl;


Your for loop is of course also doing 3 instead of 2...overflow...
}

Produced Output:

That is odd results, but the output of your program is undefined so...
Ok, the first three lines are fine. But why does the for-statement not
work as it is intented to??? It should produce exactly the same output
as the "manual" cout-statements.
I guess the problem is "ds.Tel". What's wrong in that little
program?


buffer overflow at the least. I don't see anything else wrong.
 
D

Default User

Markus said:
Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

Why are you using char buffers instead of std::string? Especially fixed
width buffers, which often waste space and are subject to buffer
overflow. As others have pointed out, that's what your problem is.




Brian Rodenborn
 
R

Rolf Magnus

Markus said:
Hi!
I've the follwoing in my C/C++-Program:

There's a struct defined as following:

struct t_datensatz {
char flag;
char ID;
char Tel[29];
};

.
.

int main void() {

The above makes me suspicious. Is that really the exact code you tried?
t_datensatz ds[2];

ds[0].flag='1';
ds[0].ID='2';
strcpy(ds[0].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmA");

You're writing one beyond the end of ds[0].Tel. It can hold 29 elements,
but you're feeding 30 to it. Don't forget the '\0' character that is
automatically appended to string literals.
cout << "OUT: " << ds[0].flag << ds[0].ID << ds[0].Tel << endl;

ds[1].flag='3';
ds[1].ID='4';
strcpy(ds[1].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmB");
cout << "OUT: " << ds[1].flag << ds[1].ID << ds[1].Tel << endl;

ds[2].flag='5';
ds[2].ID='6';
strcpy(ds[2].Tel,"TelegrammmmmmmmmmmmmmmmmmmmmC");
cout << "OUT: " << ds[2].flag << ds[2].ID << ds[2].Tel << endl;

Again, you're writing one beyond an array, this time it's the array ds,
which can hold 2 elements, but you try to write 3 of them into it.
// This doesn't work, why??
for(int i=0;i<=2;i++)
^^
Always think twice if the end condition for the loop counter contains a
<=. Your array only can hold 2 elements, from ds[0] to ds[1], so your
loop counter must stop before 2, i.e. it must be 'i<2'.
cout << "OUT: " << ds.flag << ds.ID << " " <<
ds.Tel <<
endl;

}
 

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,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top