memset

  • Thread starter =?ISO-8859-1?Q?Daniel_Sch=FCle?=
  • Start date
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

Hello

given the signature of memset function

void *memset(void *s, int c, size_t n);

what sense does it make to have c as an integer
if only the least significant byte is written
(I tried 0xFFFFFFFF und 0xFFFFFF00)

Thx in advance

Daniel
 
W

Walter Roberson

given the signature of memset function
void *memset(void *s, int c, size_t n);
what sense does it make to have c as an integer
if only the least significant byte is written

memset() dates from the time when characters and shorts
were always promoted to integers for function calls. Changing the
interface definition now would likely break code.
 
E

Emmanuel Delahaye

Daniel Schüle wrote on 26/04/05 :
Hello

given the signature of memset function

void *memset(void *s, int c, size_t n);

what sense does it make to have c as an integer
if only the least significant byte is written
(I tried 0xFFFFFFFF und 0xFFFFFF00)

Because 'char' parameter don't even exist. The minimum size for a
parameter is int. If you declare char, actually, it is converted
(promoted) to int. Smart guys (like the ones who have designed the
standard C library) write 'int' instead of 'char' (in a context
parameter). In addition, you can decide clearly if the parameter is
signed or not (int vs unsigned) and the generated code is probably more
compact in many cases.

It is probably the reason why a character has the type int in C.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
E

Eric Sosman

Emmanuel said:
Walter Roberson wrote on 26/04/05 :



Is it different now ?

Yes, in the presence of a prototype.

int knr(x) short x; { ... }
int c9x(short x) { ... }
...
short s = 42;
knr(s);
c90(s);

In the call to knr(), `s' is promoted to int and passed
to the function, where it is then "demoted" back to short
again. In the call to c9x(), `s' is passed unchanged (or
"as if unchanged"), without promotion/demotion.

This leads to a nasty little trap that occasionally
snags people who are writing C9x prototypes for K&R-style
functions, without changing the function definitions (a
practice of dubious merit). The correct C9x declaration
for knr() is not `int knr(short)' but `int knr(int)', and
a too-mechanical prototype generator that produced the
former would be wrong.
 
F

Flash Gordon

Emmanuel said:
Walter Roberson wrote on 26/04/05 :


Is it different now ?

If the function is defined with a prototype, I believe it is. Default
argument promotions only apply when there is no prototype.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top