memset

This is a generic C/C++ question regarding memset (not QNX specific)

void *memset( void *dst, int c, size_t length);

I just notice c is defined as int, but only first byte of the value is used.
Hence if I memset (buf, -2, sizeof(buf) ) and buf is a array of int each int
value will be equal to 0xfefefefe and not 0x000000fe. Although I’ve always
knows memset fills memory per bytes, it seems to me “int c” is counter
intuitive and should be “unsigned char”.

Comments?

  • Mario

HI, that is true, I never had a problem with the “int” definition but
you seem to be right. Might this be something for the POSIX.1 team at
IEEE?
Jörg

Mario Charest schrieb:

This is a generic C/C++ question regarding memset (not QNX specific)

void *memset( void *dst, int c, size_t length);

I just notice c is defined as int, but only first byte of the value is used.
Hence if I memset (buf, -2, sizeof(buf) ) and buf is a array of int each int
value will be equal to 0xfefefefe and not 0x000000fe. Although I’ve always
knows memset fills memory per bytes, it seems to me “int c” is counter
intuitive and should be “unsigned char”.

Comments?

  • Mario

http://www.ibk-consult.de

Mario Charest postmaster@127.0.0.1 wrote:

This is a generic C/C++ question regarding memset (not QNX specific)

void *memset( void *dst, int c, size_t length);

I just notice c is defined as int, but only first byte of the value is used.
Hence if I memset (buf, -2, sizeof(buf) ) and buf is a array of int each int
value will be equal to 0xfefefefe and not 0x000000fe. Although I’ve always
knows memset fills memory per bytes, it seems to me “int c” is counter
intuitive and should be “unsigned char”.

The ISO C standard says it’s int. Just like the argument to putc() and
a lot of other functions. Probably for historic reasons, but in many
cases, it also makes the function compatible with what the compiler
assumes when you don’t have a prototype.

\

Wojtek Lerch QNX Software Systems Ltd.