understanding memset

P

pauldepstein

This is copy-pasted from cplusplus.com:

BEGINNING OF QUOTE

void * memset ( void * buffer, int c, size_t num );

Fill buffer with specified character.
Sets the first num bytes pointed by buffer to the value specified by
c parameter

END OF QUOTE

Since we are filling the buffer with a specified _character_, why is
the 2nd parameter an int instead of a char?

Yes, I know that chars are really ints but it still seems a confusing
definition, and I'd greatly appreciate it if someone could explain why
the authors of this function didn't choose type char for the 2nd
parameter.

Also, could someone explain what happens when c == 0?

Thank you,

Paul Epstein
 
K

Kai-Uwe Bux

This is copy-pasted from cplusplus.com:

BEGINNING OF QUOTE

void * memset ( void * buffer, int c, size_t num );

Fill buffer with specified character.
Sets the first num bytes pointed by buffer to the value specified by
c parameter

END OF QUOTE

Since we are filling the buffer with a specified _character_, why is
the 2nd parameter an int instead of a char?

You mean unsigned char: memset() silently converts its second argument to
unsigned char and uses the result of the conversion. cplusplus.com is not
quite accurate in this regard. You may want to check the C standard.

Yes, I know that chars are really ints

Are they? I thought, char was an independent type.
but it still seems a confusing
definition, and I'd greatly appreciate it if someone could explain why
the authors of this function didn't choose type char for the 2nd
parameter.

C++ inherits memset and its relatives from the C standard. This is the
answer as to why C++ has this definition. Why the C folks formalized it
this way, I don't know.
Also, could someone explain what happens when c == 0?

According to the definition you quoted, the first num characters in the
array pointed to by buffer will be set to 0.


Best

Kai-Uwe Bux
 
D

defendusa2

A smart alec answer would be because the people who defined the method
didn't know what they were doing. A more likely answer would be that
there is some historical reason (maybe char didn't exist?) and maybe
they really didn't care.

There are a lot of places in the win32 and C++ runtime library where
things should be const char * and the "const" is missing. There was
evidently some sloppyness on someones part.
 
I

Ivan Vecerina

: This is copy-pasted from cplusplus.com:
:
: BEGINNING OF QUOTE
:
: void * memset ( void * buffer, int c, size_t num );
:
: Fill buffer with specified character.
: Sets the first num bytes pointed by buffer to the value specified by
: c parameter
:
: END OF QUOTE
:
: Since we are filling the buffer with a specified _character_, why is
: the 2nd parameter an int instead of a char?

What the function really does is store (unsigned char)c
at every memory address in [ buffer .. buffer+num )


: Yes, I know that chars are really ints but it still seems a confusing
: definition, and I'd greatly appreciate it if someone could explain why
: the authors of this function didn't choose type char for the 2nd
: parameter.

I think because in early C code char and int parameters were
automatically promoted to int (think of a time where you could
call a function without the compiler having seen its declaration).

: Also, could someone explain what happens when c == 0?

All memory bits in the addressed range will be set to zero.
Integer variables stored within the address range will
reliably be set to zero. On most platforms, pointers
and floating-point values will also be set to NULL / 0.0,
although this is not required to work (IIRC the Standard).

Any call to memset will result in undefined behavior if any
const or non-POD object is stored within the range (e.g. any
instance of a type that has a constructor, or stores a reference).

hth -Ivan
 
F

Frederick Gotham

Since we are filling the buffer with a specified _character_, why is
the 2nd parameter an int instead of a char?


In C, a character literal, e.g. 'a', is of the type "int". You'll notice
that toupper, tolower, etc. also take an "int".

Yes, I know that chars are really ints


NOT in C++. In C++, a character literal has the type "char".

but it still seems a confusing definition, and I'd greatly appreciate it
if someone could explain why the authors of this function didn't choose
type char for the 2nd parameter.


"int" was used for characters so that a function could return such things
as EOF (End Of File).

Also, could someone explain what happens when c == 0?


The byte in memory gets set to zero.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top