473,698 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Doubt in memcpy() and memset()

Hi,
I want to know some of the situations , when to use memcpy() and when
to memset();

Thanx in Advance..

Dec 6 '06 #1
12 3906

Shhnwz.a wrote:
Hi,
I want to know some of the situations , when to use memcpy() and when
to memset();

Thanx in Advance..
NAME
memcpy - copy memory area

NAME
memset - fill memory with a constant byte

HOMEWORKALITY!

[hint: man pages are fun]

Tom

Dec 6 '06 #2
Shhnwz.a wrote:
I want to know some of the situations , when to use memcpy() and when
to memset();
Can't you tell from their descriptions?

memcpy( void *s1, const void *s2, size_t n );

... copies n characters from the object pointed to by s2
into the object pointed to by s1. If copying takes place
between objects that overlap, the behaviour is undefined.

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

... copies the value of c (converted to an unsigned char)
into each of the first n characters of the object pointed
to by s.

Use whichever one fits your situation.

--
Chris "Perikles triumphant" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Dec 6 '06 #3
Chris Dollin wrote:
>
memset( void *s, int c, size_t n );

... copies the value of c (converted to an unsigned char)
into each of the first n characters of the object pointed
to by s.
By the way, I'm puzzled about the type of the second parameter.
Why not use (unsigned char) type?
memset(void *s, unsigned char c, size_t n);

Thanks.

Dec 6 '06 #4

Dead Loop wrote:
Chris Dollin wrote:

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

... copies the value of c (converted to an unsigned char)
into each of the first n characters of the object pointed
to by s.

By the way, I'm puzzled about the type of the second parameter.
Why not use (unsigned char) type?
memset(void *s, unsigned char c, size_t n);
Backwards compatibility.

Both memcpy() and memset() were defined back in the day of K&R C. In
the K&R C definition, char was automatically promoted to int when used
as a function argument.

Dec 6 '06 #5
2006-12-06 <11************ **********@j72g 2000cwa.googleg roups.com>,
Lew Pitcher wrote:
>
Dead Loop wrote:
>Chris Dollin wrote:
>
memset( void *s, int c, size_t n );

... copies the value of c (converted to an unsigned char)
into each of the first n characters of the object pointed
to by s.

By the way, I'm puzzled about the type of the second parameter.
Why not use (unsigned char) type?
memset(void *s, unsigned char c, size_t n);

Backwards compatibility.

Both memcpy() and memset() were defined back in the day of K&R C. In
the K&R C definition, char was automatically promoted to int when used
as a function argument.
yeah, but the size argument changed types. certainly nothing promotes to
a size_t, and there's no guarantee that size_t doesn't promote to
something.
Dec 6 '06 #6
Random832 wrote:
yeah, but the size argument changed types. certainly nothing promotes to
a size_t, and there's no guarantee that size_t doesn't promote to
something.
Does it matter? Most platforms won't let you push a char on the stack
anyways (disregarding the fact the standard makes no mention of
"stack").

So from a performance point of view it's moot.

Tom

Dec 6 '06 #7
Tom St Denis wrote:
>
Random832 wrote:
yeah, but the size argument changed types. certainly nothing promotes to
a size_t, and there's no guarantee that size_t doesn't promote to
something.

Does it matter? Most platforms won't let you push a char on the stack
anyways (disregarding the fact the standard makes no mention of
"stack").

So from a performance point of view it's moot.
No, but if you are passing an int rather than unsigned char, it
may need to zero-fill the rest of the int before passing it.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Dec 6 '06 #8
Kenneth Brody wrote:
No, but if you are passing an int rather than unsigned char, it
may need to zero-fill the rest of the int before passing it.
unsigned char x;
int ix;

x = 4;
ix = x;

What am I missing?

totally legal conversion provided you keep the values in x to the
portable range.

Tom

Dec 6 '06 #9
"Tom St Denis" <to********@gma il.comwrites:
Kenneth Brody wrote:
>No, but if you are passing an int rather than unsigned char, it
may need to zero-fill the rest of the int before passing it.

unsigned char x;
int ix;

x = 4;
ix = x;

What am I missing?

totally legal conversion provided you keep the values in x to the
portable range.
Certainly it's legal. The question is performance (though in practice
I don't think it's a significant concern).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 6 '06 #10

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

Similar topics

3
2818
by: cylin | last post by:
Dear all, When I use memcpy second time, it seems to be no use. There should be two "Hello,world!" strings in this buffer. I don't know what's wrong? Please help, thanks. Regards, cylin. ----------------------------------------------
7
33340
by: bisforbenson | last post by:
I'm trying to compile things with gcc 4. After running the following command: % gcc -O3 -g -Wall -Wno-unused -DLINUX -D_REENTRANT -o ../../obj/egi.o -I../../include/ -I... -c egi.cpp i get the following errors: /usr/lib/gcc/i386-redhat-linux/4.0.1/../../../../include/c++/4.0.1/cstring:79: error: '::memcpy' has not been declared
20
2310
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
22
3149
by: srivatsan_b | last post by:
Hi, Can somebody explain whether an explicit typecast is mandatory while calling memset function for a structure? like in the following code snapshot..... struct some_structure x; memset((some_structure*)&x,0,sizeof(some_structure)); Will memset(&x,0,sizeof(some_structure)); cause some issues? Thanks in advance
2
2353
by: Sidney Zhang | last post by:
Hi: We need to manipulate data block in C#, is there anything in C# similiar as memcpy/memset in C++? copy bytes one by one is really a night mare for me. Thanks Sidney
16
2801
by: Jeff | last post by:
Im trying to memcpy a buffer from a filled in simple structure. When I memcpy and then print the resulting buffer, I see 7 locations that have junk before my data starts. My data structure is: struct command_pkt { char command_num; char command; }; typedef command_pkt COMMAND;
3
6840
by: Alex Vinokur | last post by:
Is using memcpy() with queue<char> safe? ------ C++ code ------ #include <cstring> #include <iostream> #include <queue> using namespace std; int main() {
9
10250
by: barcaroller | last post by:
Is it safe to use the result of 'new int' in a memcpy(). Example: int* cpp_block = new int; int* c_block = some_c_function(); memcpy(cpp_block, c_block, 100);
18
2699
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.