In article <. com>,
danu <> wrote:
>Basically I'm trying to do here is put the uint32_t seq_num into the
>first 4 bytes of the buf. But every time I do this, it wouldn't get
>copied in to the buf at all. Can anyone point out the problem. Thanks a
>lot.
>char buf[packet_size];
>uint32_t seq_num = 0;
> uint32_t packet_num = 0;
>
>while((nread = read(fd, buf+8, packet_size-
) > 0) {
> packet_num++;
> seq_num = htonl(packet_num); /* htonl: used to convert the
>actual packet_num into network numbers*/
htonl() takes as its argument an unsigned long, which might not be
the same size as uint32_t.
> memcpy(buf, &seq_num, 4);
uint32_t will be 32 bits if a 32 bit unsigned type exists (and should be
an error otherwise I believe), but a 32 bit unsigned type is not
necessarily 4 bytes long. Suppose for example you are on a DSP in
which char is 32 bits; sizeof(char) is promised to be 1 by the standard,
so on such a system, memcpy() of 4 bytes would be copying 4*32 bits.
>....
>....
>}
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest