string class problem

V

vertigo

Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Thanx
Michal
 
J

John Harrison

vertigo said:
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/
int len;
}
struct packet *p = new struct packet;

packet* p = new packet;

struct is not needed here.
p->type=10;
p->len=0;

std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);

i want to create std::string object which has bytes from p.
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?


Prevent what exactly? If you want to prevent that values displaying as less
then 0 then cast to unsigned char before displaying them.
Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

I think you are asumming that packet is five bytes big. But it looks like
the compiler is adding padding bytes between type and len and those strange
values you see are the padding bytes. Try doing this

cout << sizeof (packet);

I'd bet that you get 8 not 5 because the compiler has added 3 padding bytes.

There is no standard way to stop a compiler adding padding bytes, you should
look at you compiler documentation to see if it has a way of stopping them,
most compilers do. Alternatively rewrite you code so you aren't depending on
whether a compiler adds padding bytes or not.

john
 
G

Gernot Frisch

There is no standard way to stop a compiler adding padding bytes,
you should
look at you compiler documentation to see if it has a way of
stopping them,
most compilers do. Alternatively rewrite you code so you aren't
depending on
whether a compiler adds padding bytes or not.

in gcc and visualC use:
#pragma push(pack, 1)
struct blah
{
};
#pragma pop(pack)
 
G

Gernot Frisch

str=*((byte*)p+i);

Oh, totally overseen:
str = (byte) (*(byte*)p+i)

because:
(byte*)p
is a pointer to a byte
(byte*)p + i
is a integer number

The casting comes first!
-Gernot
 
V

Victor Bazarov

vertigo said:
Hello
I have:
struct packet{
byte type; /*my byte is unsigned char*/

It would be easier (and clearer) to write

typedef unsigned char byte;

than the comment. It would make your program even closer to being
compilable.
int len;
}
;

struct packet *p = new struct packet;

You may drop the 'struct' here.
p->type=10;
p->len=0;

Shouldn't this rather be done by a constructor? Never mind.
std::string str="";
for (int i=0; i<5; i++)
str=*((byte*)p+i);


The 'str' has no storage. It's _empty_. Attempt to access its
characters causes undefined behaviour.

Now, taking 'p' and converting it to 'byte*' and then trying to
dereference that pointer _indexed_ somehow is true undefined
behaviour. What are you trying to achieve here?
i want to create std::string object which has bytes from p.

std::vector said:
(because that string object is later used for Sending() that packet).
My problem is that: when before sending i displayed str some character
values were less than 0 (thru to char instead of unsigned char).
How can i prevent it ?

Prevent what, exactly? 'std::string' is 'std::basic_string<char>'.
When you put values of type 'byte' there, it's possible that they will
be negative (because on your system 'char' is signed, most likely).
If you want your "string" to have unsigned characters, you need to
instantiate the template 'std::basic_string' yourself:

std::basic_string<byte> str;

Second problem is strange. When i displayes str i received:
10, -25, 115, 0, 0.
Why there are -25 and 115 insted of 0, 0 ?

Because there is probably padding between the 'type' and 'size'
members of your 'Packet'.

V
 

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
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top