473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

understanding memset

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

Aug 21 '06 #1
4 5019
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.

---------------------------------------------
bumperstickers:http://www.cafepress.com/bush_doggers?pid=2794571
http://photos1.blogger.com/blogger/1...rvssoldier.jpg

pa**********@att.net wrote:
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
Aug 21 '06 #2
pa**********@att.net wrote:
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
Aug 21 '06 #3
<pa**********@att.netwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Aug 21 '06 #4
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.

--

Frederick Gotham
Aug 22 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mantorok Redgormor | last post by:
When is it that memset caused problems? I recall from posts in the past where someone used memset in their code that invoked undefined behavior. What about the following? char the_array; ...
6
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the...
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
27
by: volunteers | last post by:
I met a question about memset and have no idea right now. Could anybody give a clue? Thanks memset is sometimes used to initialize data in a constructor like the example below. What is the...
23
by: AndersWang | last post by:
Hi, dose anybody here explain to me why memset would be faster than a simple loop. I doubt about it! In an int array scenario: int array; for(int i=0;i<10;i++) //ten loops
18
by: dykeinthebox | last post by:
Consider the following program: #include <stdlib.h> #include <string.h> int main( void ) { void *p = malloc( 4 ); if ( p ) {
18
by: Gaijinco | last post by:
I'm having a headache using memset() Given: int v; memset((void*)v, 1, sizeof(v)); Can I be 100% positive than v = 1 for i 0, or there is something else I have to do?.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.