473,734 Members | 2,724 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
12 3913
Tom St Denis wrote:
>
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.
I may be mistaken here, but...

extern void foo(unsigned char c);
extern void bar(int i);

void foobar()
{
unsigned char c = 'x';

foo(c);
bar(c);
}

Here, the call to foo() can push the single byte of c (if the system
allows one to push a byte), or load the byte into a register and then
push the register. However, the call to bar() requires that the byte
be loaded into a register, while zero-filling the register prior to
pushing it on the stack.

Perhaps something like:

load al,[c]
push eax
call foo

xor eax,eax
load al,[c]
push eax
call bar

(Yes, this example assumes the concept of registers and a stack.)

--
+-------------------------+--------------------+-----------------------+
| 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 7 '06 #11
Kenneth Brody wrote:
>
.... snip ...
>
Here, the call to foo() can push the single byte of c (if the system
allows one to push a byte), or load the byte into a register and then
push the register. However, the call to bar() requires that the byte
be loaded into a register, while zero-filling the register prior to
pushing it on the stack.
There is no reason to assume the system has either registers or a
stack. These are not specified in the C standard. In fact, I can
think of systems that dispense with at least one of those concepts
entirely, and handle C perfectly well.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Dec 8 '06 #12
CBFalconer wrote:
>
Kenneth Brody wrote:
... snip ...

Here, the call to foo() can push the single byte of c (if the system
allows one to push a byte), or load the byte into a register and then
push the register. However, the call to bar() requires that the byte
be loaded into a register, while zero-filling the register prior to
pushing it on the stack.

There is no reason to assume the system has either registers or a
stack. These are not specified in the C standard. In fact, I can
think of systems that dispense with at least one of those concepts
entirely, and handle C perfectly well.
Yes, I realize that. I was just using register-and-stack-based
system as an example of the difference between passing an int and
an unsigned char to a function, as noted in my last sentence:
(Yes, this example assumes the concept of registers and a stack.)
--
+-------------------------+--------------------+-----------------------+
| 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 8 '06 #13

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

Similar topics

3
2821
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
33390
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
2317
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
3156
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
2354
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
2812
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
6852
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
10260
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
2704
by: sam | last post by:
(newbie)Technically what's the difference between memset() and memcpy() functions?
0
8946
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
8776
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
9310
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
6735
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...
0
6031
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.