使用memcpy()从内存传输到变量 [英] Use of memcpy() to transfer from memory to a variable

查看:85
本文介绍了使用memcpy()从内存传输到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我不想进入的原因,我需要从1到3个字节转移到

变量,我知道它是4个字节长。未写入4字节
目标变量的字节必须为零。以下是使用memcpy()a

如此明确的方式吗?代码是在这个例子中知道

sizeof(unsigned long)== 4。代码有点做作

为了提供一个自包含的程序,它将编译并显示我询问的memcpy()使用的




以下代码清理编译使用


gcc -Wall -ansi -pedantic


其中


gcc -dumpversion显示


4.10。

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main(无效)

{


unsigned long值;

unsigned char * pAddress;

unsigned char rem;


pAddress = malloc(3);

if(pAddress == NULL)

{

puts(" malloc failed") ;

退出(EXIT_FAILURE);

}


/ *加载一些任意值* /

pAddress [0] = 1;

pAddress [1] = 2;

pAddress [2] = 4;

rem = 3 ; / *硬连线演示 - 通常计算* /


if(rem)

{

value = 0;

memcpy(& value,pAddress,rem);

}


/ *用于演示 - 显示pAddress中的值已经过转移* /

printf(" value = 0x%0lX \ n",value);


返回0;

}


-

马丁

For reasons I won''t go into, I need to transfer from 1 to 3 bytes to a
variable that I know is 4 bytes long. Bytes not written to in the 4-byte
target variable must be zero. Is the following use of memcpy() a
well-defined way of so doing? The code is written knowing that
sizeof(unsigned long) == 4 in this instance. The code is somewhat contrived
in order to provide a self-contained program that will compile and show the
use of memcpy() I am asking about.

The following code clean compiles using

gcc -Wall -ansi -pedantic

where

gcc -dumpversion displays

4.10.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{

unsigned long value;
unsigned char *pAddress;
unsigned char rem;

pAddress = malloc(3);
if ( pAddress == NULL )
{
puts("malloc failed");
exit(EXIT_FAILURE);
}

/* load in some arbitrary values */
pAddress[0] = 1;
pAddress[1] = 2;
pAddress[2] = 4;
rem = 3; /* hard-wired for demo - normally calculated */

if ( rem )
{
value = 0;
memcpy( &value, pAddress, rem );
}

/* for demo - shows values in pAddress have been transferred */
printf("value = 0x%0lX\n", value);

return 0;
}

--
Martin

推荐答案

[在一个模型文章中,为了简洁起见,我已经剪了很多但是请确认

看到原文而不是从遗漏中推断出他的错误

on mine] ......


马丁说:
[In a model post, much of which I''ve snipped for brevity but please do
see the original rather than deducing fault on his part from omissions
on mine]...

Martin said:

由于我不想进入的原因,我需要从1转移到3个字节到一个

变量,我知道它是4个字节长。未写入

4字节目标变量的字节必须为零。以下是使用memcpy()

这样做的明确方法吗?
For reasons I won''t go into, I need to transfer from 1 to 3 bytes to a
variable that I know is 4 bytes long. Bytes not written to in the
4-byte target variable must be zero. Is the following use of memcpy()
a well-defined way of so doing?



不,但它也没有完全未定义。

No, but it''s not exactly undefined either.


unsigned long value;

unsigned char * pAddress;

unsigned char rem;


pAddress = malloc(3);

if(pAddress == NULL)

{
unsigned long value;
unsigned char *pAddress;
unsigned char rem;

pAddress = malloc(3);
if ( pAddress == NULL )
{



到目前为止一直很好(和< snip>)

So far so good (and <snip>)


/ *加载一些任意值* /

pAddress [0] = 1;

pAddress [1] = 2;

pAddress [2] = 4;


rem = 3; / *硬连线演示 - 通常计算* /


if(rem)

{

value = 0;

memcpy(& value,pAddress,rem);
/* load in some arbitrary values */
pAddress[0] = 1;
pAddress[1] = 2;
pAddress[2] = 4;
rem = 3; /* hard-wired for demo - normally calculated */

if ( rem )
{
value = 0;
memcpy( &value, pAddress, rem );



好​​的,这肯定是合法的,因为sizeof(unsigned long)是4,因为

在你的文章中说明。


但你到底得到了什么?答:这取决于与您的实现有关的字节顺序

。如果你有小尾数

整数,你会得到一个结果,如果你有大端,你会得到

另一个。当然还有各种各样的中端口味。


所以,如果你对价值包含的价值不太挑剔,或者如果

你很高兴你的实施是正确的,而且你没有担心移植,你很好。


HTH,HAND。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Okay, this is certainly legal, given that sizeof(unsigned long) is 4, as
stated in your article.

But what do you actually get? Answer: it depends on the byte ordering
that pertains to your implementation. If you have little endian
integers, you''ll get one result, and if you have big-endian, you''ll get
another. And of course there are various flavours of middle-endian.

So if you''re not too fussy about what value ''value'' contains, or if
you''re happy that it''s correct on your implementation and you''re not
worried about porting, you''re fine.

HTH, HAND.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




" Martin" < martin.o_brien @ [no-spam] which.netwrote in message

news:Zv ***************** @ fe168.usenetserver.com ...

"Martin" <martin.o_brien@[no-spam]which.netwrote in message
news:Zv*****************@fe168.usenetserver.com...

由于我不想进入的原因,我需要从1到3个字节转移到

变量我知道是4个字节长。未写入4字节
目标变量的字节必须为零。以下是使用memcpy()a

如此明确的方式吗?
For reasons I won''t go into, I need to transfer from 1 to 3 bytes to a
variable that I know is 4 bytes long. Bytes not written to in the 4-byte
target variable must be zero. Is the following use of memcpy() a
well-defined way of so doing?



memcpy()复制字节,因此它将始终在

结果中产生相同的位模式。正如其他人所说,取决于结束。这可能是
不同的整数值。我只是想知道是否使用带有union的结构可能会产生更多不可理解的代码吗?

memcpy() copies bytes so it will always produce the same bit pattern in the
result. As others have said, depending on the "endianess" this may yeild
different integer values. I just wonder if using a struct with a union may
produce more unnderstandable code?


代码写入时知道

sizeof(unsigned long)== 4在这个例子中。代码有点
The code is written knowing that
sizeof(unsigned long) == 4 in this instance. The code is somewhat



人为

contrived


为了提供一个自包含的程序,将编译和show
in order to provide a self-contained program that will compile and show




the


使用memcpy()我问的问题。


以下代码清理编译使用


gcc -Wall -ansi -pedantic


其中


gcc -dumpversion显示


4.10。


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main(无效)

{


unsigned long value;

unsigned char * pAddress;

unsigned char rem;


pAddress = malloc(3);

if(pAddress == NULL)

{

puts(" malloc failed" ;);

退出(EXIT_FAILURE);

}


/ *加载一些任意值* /

pAddress [0] = 1;

pAddress [1] = 2;

pAddress [2] = 4;


rem = 3; / *硬连线演示 - 通常计算* /


if(rem)

{

value = 0;

memcpy(& value,pAddress,rem);

}


/ *用于演示 - 显示pAddress中的值已经过转移* /

printf(" value = 0x%0lX \ n",value);


返回0;

}


-

Martin
use of memcpy() I am asking about.

The following code clean compiles using

gcc -Wall -ansi -pedantic

where

gcc -dumpversion displays

4.10.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{

unsigned long value;
unsigned char *pAddress;
unsigned char rem;

pAddress = malloc(3);
if ( pAddress == NULL )
{
puts("malloc failed");
exit(EXIT_FAILURE);
}

/* load in some arbitrary values */
pAddress[0] = 1;
pAddress[1] = 2;
pAddress[2] = 4;
rem = 3; /* hard-wired for demo - normally calculated */

if ( rem )
{
value = 0;
memcpy( &value, pAddress, rem );
}

/* for demo - shows values in pAddress have been transferred */
printf("value = 0x%0lX\n", value);

return 0;
}

--
Martin



Martin写道:
Martin wrote:

由于我不想进入的原因,我需要从1到3个字节转移到我知道的

变量是4个字节长。未写入4字节
目标变量的字节必须为零。以下是使用memcpy()a

如此明确的方式吗?代码是在这个例子中知道

sizeof(unsigned long)== 4。为了提供一个自包含的程序来编译

并且显示我正在询问的memcpy()的使用,代码有点为b $ b b b。
For reasons I won''t go into, I need to transfer from 1 to 3 bytes to a
variable that I know is 4 bytes long. Bytes not written to in the 4-byte
target variable must be zero. Is the following use of memcpy() a
well-defined way of so doing? The code is written knowing that
sizeof(unsigned long) == 4 in this instance. The code is somewhat
contrived in order to provide a self-contained program that will compile
and show the use of memcpy() I am asking about.



那么像

这样的问题是什么?b $ b unsigned long value =

( unsigned long)byteA

| ((unsigned long)byteB<< 8)

| ((unsigned long)byteC<< 16)

;


其中字节A,B和C是您要传输到的字节数

值的低,中,高和中间部分?


[假定为8位字节。]


优点(a)绕过字节序问题(b)不需要使用`memcpy`来解决




-

签名Hedgehog

花了很长时间,比最慷慨的估计要长得多。

- James White,/ Sector一般/

So what''s wrong with something like

unsigned long value =
(unsigned long) byteA
| ((unsigned long) byteB << 8)
| ((unsigned long) byteC << 16)
;

where bytes A, B, and C are the bytes you want to transfer to
the low, low-middle, and high-middle parts of `value`?

[This assumes 8-bit bytes.]

Advantages (a) bypasses endianness issues (b) no need to muck
around with `memcpy`.

--
Signed Hedgehog
"It took a very long time, much longer than the most generous estimates."
- James White, /Sector General/


这篇关于使用memcpy()从内存传输到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆