473,695 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(newbie)Technic ally what's the difference between memset() andmemcpy() functions?

sam
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
Sep 10 '08 #1
18 2698
sam wrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
You could read the manual ...

memset() sets a memory area to a certain value
memcpy() copies one memory area to another area

Bart
Sep 10 '08 #2
On Sep 10, 8:59*pm, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
just as the naming of the funcation.

memset is used to *set* a range of memory to the unique value.

#define BUFSIZE 1024
char buf[BUFSIZE];
memset(buf, 0, sizeof(BUFSIZE) ); /* set [buf, buf+BUFSIZE) range
memory to 0 */

whereas, memcpy is used to *copy* a range of memory into another
range of memory.

char buf1[BUFSIZE];
char buf2[BUFSIZE] = { 'a', 'b', 'c' };

memcpy(buf1, buf2, sizeof(buf2)); /* copy the value of buf2 into buf1
*/

Roadt
Sep 10 '08 #3
On Sep 10, 2:59 pm, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
Well, formally, they do different things. Practically, who
cares? You'd never use either in a C++ program.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 10 '08 #4
On Sep 10, 9:34*am, James Kanze <james.ka...@gm ail.comwrote:
On Sep 10, 2:59 pm, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?

Well, formally, they do different things. *Practically, who
cares? *You'd never use either in a C++ program.
Not necessarily. If you are using a library with a C API, and they
require a zeroed POD struct as a param, memset is your easiest bet.
Sep 10 '08 #5
On 10 Sep, 13:59, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
A more interesting question would be - what's the difference between
memcpy() and memmove()?

The answer to this one is that memcpy can go wrong if the place it's
moving from and the place it's moving to overlap. As a simple example,
suppose you ask it to copy 20 bytes from addresses [100-119] to
addresses [105-124]. It might copy the contents of [100] to [105],
then the contents of [101] to [106], etc, but when it gets to [105],
instead of copying what was originally in [105], this has already been
overwritten and so what it reads, and copies to [110], is what was
originally in [100]. So instead of getting a copy of what was in
[100-119], you get four copies of what was in [100-105].

memmove() is guaranteed not to go wrong in this way.

Hope that helps.
Paul.
Sep 10 '08 #6
On Sep 10, 10:20 pm, red floyd <redfl...@gmail .comwrote:
On Sep 10, 9:34 am, James Kanze <james.ka...@gm ail.comwrote:
On Sep 10, 2:59 pm, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?
Well, formally, they do different things. Practically, who
cares? You'd never use either in a C++ program.
Not necessarily. If you are using a library with a C API, and
they require a zeroed POD struct as a param, memset is your
easiest bet.
The problem is that memset doesn't necessarily zero the
structure correctly. The correct solution to the above (even in
C) is to declare a static instance of the struct, and assign it
to the struct you want to zero.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 11 '08 #7
In article <a1************ *************** *******@r66g200 0hsg.googlegrou ps.com>,
red floyd <re******@gmail .comwrote:
>On Sep 10, 9:34*am, James Kanze <james.ka...@gm ail.comwrote:
>On Sep 10, 2:59 pm, sam <sameer...@gmai l.comwrote:
(newbie)Technic ally what's the difference between memset() and
memcpy() functions?

Well, formally, they do different things. *Practically, who
cares? *You'd never use either in a C++ program.

Not necessarily. If you are using a library with a C API, and they
require a zeroed POD struct as a param, memset is your easiest bet.
Huh?

Have you tried:

struct MyPodStruct; // define it somewhere
MyPodStruct s = {0};
Read about structure initialisation and remove all these pointless
memset from your code. For example:
http://www.informit.com/guides/conte...lus&seqNum=171

I think you can also do

MyPodStruct s = {};

but I seem to be unable to find precise information if there is a
difference between the two and I don't have access to the standard at
the moment. I don't think so since if there are too few initialisers,
the remaining are assumed to be 0. Anyone?

Yannick


Sep 11 '08 #8
In article <6a************ *************** *******@r66g200 0hsg.googlegrou ps.com>,
<gw****@aol.com wrote:
>On 10 Sep, 13:59, sam <sameer...@gmai l.comwrote:
>(newbie)Techni cally what's the difference between memset() and
memcpy() functions?

A more interesting question would be - what's the difference between
memcpy() and memmove()?

The answer to this one is that memcpy can go wrong if the place it's
moving from and the place it's moving to overlap. As a simple example,
suppose you ask it to copy 20 bytes from addresses [100-119] to
addresses [105-124]. It might copy the contents of [100] to [105],
then the contents of [101] to [106], etc, but when it gets to [105],
instead of copying what was originally in [105], this has already been
overwritten and so what it reads, and copies to [110], is what was
originally in [100]. So instead of getting a copy of what was in
[100-119], you get four copies of what was in [100-105].

memmove() is guaranteed not to go wrong in this way.
Strictly speaking, memcpy did not go wrong. It did exactly what was
asked for which is copy byte by byte 20 bytes from address 100 to
address 105.

I would hesitate to recommend using memmove over memcpy in the general
case based on being resilient to this programming mistake because one
of the only justification to use memcpy is its efficiency. By virtue
of protecting against the above case, memmove has to be less efficient
than memcpy. Obviously, in 95% of cases you won't care about
efficiency so should use neither memcpy nor memmove but stick with
much safer things like copy constructors, assigment operator,
std::swap and std::copy.
Yannick
Sep 11 '08 #9
Yannick Tremblay wrote:
Read about structure initialisation and remove all these pointless
memset from your code. For example:
http://www.informit.com/guides/conte...lus&seqNum=171
Please, don't link to articles with such low quality. I did read up to sixth
line (excluding titles) and there were more technical errors than words.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Sep 11 '08 #10

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

Similar topics

31
2975
by: Raymond Hettinger | last post by:
Based on your extensive feedback, PEP 322 has been completely revised. The response was strongly positive, but almost everyone preferred having a function instead of multiple object methods. The updated proposal is at: www.python.org/peps/pep-0322.html In a nutshell, it proposes a builtin function that greatly simplifies reverse iteration. The core concept is that clarity comes from specifying a sequence in a forward direction and...
4
2178
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
7
23547
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead. Execution is faster. Where will it be stotred?(Is it in bss/stack/?) FUNCTION:
13
1809
by: cashdeskmac | last post by:
I am a newbie to C# and have come across some VB .NET code in my studies. I notice that VB uses functions and subs, and was wondering what the difference was. Is it that one has a return value and the other doesn't?
4
2723
by: Jorge | last post by:
Iam a newbie. What does this error message mean? A first chance exception of type 'System.ArgumentException' occurred in Microsoft.VisualBasic.dll
3
2163
by: R69D | last post by:
Hi, I'm real new to Java and need some help...was going through some old tutorials and need help with these questions: 1)What is “composition of methods?” How do u define the composition of methods? 2)What is the difference between the output from String s; System.out.println(“s = “+s); and String s = new String(); System.out.println(“s = “+s);
44
1675
by: Andy Dingley | last post by:
On 15 May, 04:55, Prisoner at War <prisoner_at_...@yahoo.comwrote: Or in another universe, where things are understood and site code is stable and reliable, beginners don't even think about reading JavaScript books until they've written some HTML and CSS. You need it _VERY_ rarely. Using it without a foundation of good HTML understanding leads to trouble. Few people can learn both from scratch simultaneously, so it's useful to really...
7
2764
by: John | last post by:
Hi I have a WinForm app with a bound form. When user enters a value in field rateid I lookup the respective rate amount from a table and assign it to field rate.I am using the DLookup function to achieve this (full code given below). This function returns a value of type object so I need to convert it to string. If I use the function as below (using ToString) it works fine; Me.txtRate.Text = DLookup(<parameters here>).ToString
6
11483
Markus
by: Markus | last post by:
Things to discuss: Headers What are they? What does PHP have to do with headers? Why can they only be sent before any output? Common causes
0
8640
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
8582
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9122
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
8832
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
5841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1980
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.