CFanatic

Go Back   CFanatic > Programming > C Programming > Advance C


  #1  
Old 05-25-2007, 05:22 AM
Junior Member
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sureshN is on a distinguished road
| More
Exclamation working of memset() function

The prototype for the function goes like this and it fills the first n bytes of the memory area pointed to be s with the constant byte c.

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

If you look at the implementation of the function, the second parameter is converted to ‘unsigned char’ so as to fill the memory with bytes.

I have few questions regarding the design of this function:

-Why does the function take integer as the parameter and later converts it into unsigned char? The function could have accepted the parameter as ‘unsigned char’ directly.

-Does it not add confusion to the user of this function to look at the parameter as integer and then pass the integer and expect the same to be filled in the memory? For example considering integer of 4 bytes, the user might pass 0xAABBCCDD to be filled in the memory bytes but the function truncates and fill 0xDD in the memory.

Any ideas or guesses?
Reply With Quote
  #2  
Old 05-25-2007, 06:56 AM
shabbir's Avatar
Administrator
 
Join Date: Sep 2006
Posts: 773
Thanks: 1
Thanked 4 Times in 4 Posts
shabbir will become famous soon enoughshabbir will become famous soon enough
| More
Re: working of memset() function

I would argue on your both the points.
Quote:
Originally Posted by sureshN
-Why does the function take integer as the parameter and later converts it into unsigned char? The function could have accepted the parameter as ‘unsigned char’ directly.
As per MSDN its Character to set to the memory and so integer means the ASCII value of the character as an input and so the valid range is always a positive and so unsigned char is as desired.

when passing 0xAABBCCDD as second parameter its not what is expected (as MSDN clearly says what it does)
Reply With Quote
  #3  
Old 05-28-2007, 04:11 AM
Junior Member
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sureshN is on a distinguished road
| More
Re: working of memset() function

Thanks Shabbir. What is concerning is the way it is designed. I am trying to understand under what circumstances this was designed when the ANSI standard was formed? My argument is why can't the second paramter be 'unsigned char' instead of an 'int'? In this way there is no confusion at all as to what range of values would the function take. Anyway it is converted to unsigned char inside. Why after so many years of forming a standard it is left the same?
Reply With Quote
  #4  
Old 05-28-2007, 04:42 AM
shabbir's Avatar
Administrator
 
Join Date: Sep 2006
Posts: 773
Thanks: 1
Thanked 4 Times in 4 Posts
shabbir will become famous soon enoughshabbir will become famous soon enough
| More
Re: working of memset() function

memset is a C function and probably because of backward compatibility.

MS decided to make C# a new language and not a have a predecessors because it did not wanted to mess the things up with the backward compatibility issue like the one you are pointing out.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Command Line Compile + Subdirectory DLL Not Working? will C# Programming 1 10-18-2008 09:41 PM
Simple class not working psppb C++ Programming 3 01-08-2008 09:24 PM
Write a value-returning function??????? Albaniankool C++ Programming 0 05-30-2007 08:52 AM
c# can you force a function to NOT be inlined? codeunlock C# Programming 0 04-29-2007 12:43 AM
WaitFor function Angus C++ Programming 0 11-11-2006 09:11 AM

 

Follow CFanatic on Twitter
CFanatic Search
Custom Search

Advertisement

Go4Expert's Job Board
Powered by

Go4Expert Newsletter
Enter your Name and Email Address below and Click Subscribe.

Subscriber Counter

Not planning to subscribe yet, browse Go4Expert's Newsletter Archive

All times are GMT -5. The time now is 03:33 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Looking for work in C, C++, C#? Sign up at Elance and search over 30,000 Freelancing jobs today.