|
From: Ioannis V. <no...@ho...> - 2003-03-04 22:59:39
|
Some adjustments:=0D
=0D
=0D
=0D
-------Original Message-------=0D
=0D
From: Ioannis Vranos=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:55:26 =F0=EC=
=0D
To: Dev-C++ Mailing List=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
-------Original Message-------=0D
=0D
From: Per Westermark=0D
Date: =D4=E5=F4=DC=F1=F4=E7, 5 =CC=DC=F1=F4=E9=EF=F2 2003 12:26:51 =F0=EC=
=0D
To: Giligun=0D
Cc: C++=0D
Subject: Re: [Dev-C++] Character variables=0D
=0D
short s =3D 'ab';=0D
int i =3D 'abcd';=0D
=0D
given that int is 4 byte.=0D
=0D
=0D
=3D=3D> That isn't valid C++. And where it gets compiled you can't consid=
er it
as safe.=0D
=0D
=0D
=0D
But a lot of compilers will give you warnings, since it you can't be sure=
=0D
if memcpy(buf,&s,sizeof(s)) will give you=0D
buf[0] =3D 'a', buf[1] =3D 'b';=0D
or=0D
buf[0] =3D 'b', buf[1] =3D 'a';=0D
=0D
In short - be very careful about using this feature. One place where it's=
=0D
elegant to use is if you are reading a tagged file, where each tag is=0D
4-byte long:=0D
char *buf;=0D
=0D
int tag =3D 0;=0D
for (i =3D 0; i < 4; i++) {=0D
tag =3D (tag << 8) | *buf++;=0D
}=0D
=0D
=0D
=3D=3D> Too system dependent. Additionally bitwise operators are to be us=
ed in
unsigned integrals only.=0D
=0D
=3D=3D> He could use wchar_t instead. But since he wants to store charact=
ers
(assuming in the range 0-127) he can use a char sequence nicely and easil=
y. =0D
=0D
=0D
switch (tag) {=0D
case 'STRT': ...; break;=0D
case 'HEDR': ...; break;=0D
case 'DATA': ...; break;=0D
case 'ENDF': ...; break;=0D
}=0D
=0D
If there are a lot of tags, the result is a very fast matching of the=0D
tags.=0D
=0D
=0D
=3D=3D> By the way i would do something like the above using std::bitset.=
=0D
=0D
=0D
=0D
Ioannis Vranos=0D
=0D
* Programming pages: http://www.noicys.freeurl.com=0D
* Alternative URL: http://run.to/noicys=20 |