473,407 Members | 2,320 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,407 software developers and data experts.

memset

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

Nov 14 '05 #1
5 3705
In article <d4**********@news2.rz.uni-karlsruhe.de>,
=?ISO-8859-1?Q?Daniel_Sch=FCle?= <uv**@rz.uni-karlsruhe.de> wrote:
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.
--
Warning: potentially contains traces of nuts.
Nov 14 '05 #2
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."

Nov 14 '05 #3
Walter Roberson wrote on 26/04/05 :
memset() dates from the time when characters and shorts
were always promoted to integers for function calls.


Is it different now ?

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

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #4


Emmanuel Delahaye wrote:
Walter Roberson wrote on 26/04/05 :
memset() dates from the time when characters and shorts
were always promoted to integers for function calls.

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.

--
Er*********@sun.com

Nov 14 '05 #5
Emmanuel Delahaye wrote:
Walter Roberson wrote on 26/04/05 :
memset() dates from the time when characters and shorts
were always promoted to integers for function calls.


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #6

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...
9
by: divya_rathore_ | last post by:
Consider the dynamic allocation of a 2D array: int **temp; temp = new int*; for (y=0; y<height; y++) temp = new int; I have 2 Questions: Does new initializes memory to 0?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.