将结构复制到更大的结构? [英] Copying a struct to a larger struct?

查看:60
本文介绍了将结构复制到更大的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用memcpy将一个结构的内容复制到另一个结构中

这是原始结构的超集(第二个结构有额外的

成员在结束)。我写了一个小程序来测试这个,并且它好像b $ b似乎工作正常,但有没有做过像

这样的事情可能会导致任何问题?


这是我写的一个小程序来测试这个:


#include< stdio.h>

int main()

{

struct simplestruct

{

int i;

char str [8];

};


struct extendedstruct

{

int i ;

char str [8];

int j;

};


struct simplestruct foostruct;

struct extendedstruct barstruct;


/ *填充simplestruct实例的成员* /

foostruct.i = 7 ;

strcpy(foostruct.str," Test");


/ *将simplestruct实例的内容复制到

extendedstruct instance * /

memcpy(& barstruct,& foostruct,sizeof(foostruct));


/ * Populat扩展结构实例的剩余成员* /

barstruct.j = 13;


/ *扩展结构实例成员的打印值* /

printf(" i \ t%d \ nttr \ t%s \ nj \ t%d \ n",barstruct.i,barstruct.str,

barstruct.j);

}


提前感谢任何建议。

-

Karl Garrison
he********@yahoo.com

I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?

Here''s the small program I wrote to test this:

#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};

struct simplestruct foostruct;
struct extendedstruct barstruct;

/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");

/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));

/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;

/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}

Thanks in advance for any advice.
--
Karl Garrison
he********@yahoo.com

推荐答案



他******** @ yahoo.com 写道:


he********@yahoo.com wrote:
struct simplestruct
{
int i;
char str [8];
};

struct extendedstruct
{
int i;
char str [8];
int j;
};
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};




较低的维护性,较少出错的方法是

struct simplest ruct {...};

struct extendedstruct {struct simplestruct s; ...};



A lower maintainence, less error prone way to do this is
struct simplestruct { ... };
struct extendedstruct { struct simplestruct s; ... };


他*** *****@yahoo.com 写道:
我想使用memcpy将一个结构的内容复制到另一个结构中
这是原始结构的超集(第二个结构在最后有额外的
成员)。我写了一个小程序来测试这个,它似乎工作得很好,但有没有做过像
这样可能导致任何问题的情况?

这里''我编写的小程序来测试它:

#include< stdio.h>

int main()
{
struct simplestruct
{
int i;
char str [8];
};

struct extendedstruct
{
int i;
char str [8];
int j;
};

struct simplestruct foostruct;
struct extendedstruct barstruct;

/ *填充simplestruct实例的成员* /
foostruct.i = 7;
strcpy(foostruct.str," Test");

/ *复制内容simplestruct实例到
扩展结构实例* /
memcpy(& barstruct,& foostruct,sizeof(foostruct));

/ *填充外部的剩余成员ndedstruct实例* /
barstruct.j = 13;

/ *打印扩展结构实例成员的值* /
printf(" i\t%d \ nstr\t%s\\\
j \ t%d \ n",barstruct.i,barstruct.str,
barstruct.j);
}

谢谢提前获取任何建议。
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?

Here''s the small program I wrote to test this:

#include <stdio.h>
int main()
{
struct simplestruct
{
int i;
char str[8];
};

struct extendedstruct
{
int i;
char str[8];
int j;
};

struct simplestruct foostruct;
struct extendedstruct barstruct;

/* Populate the members of the simplestruct instance */
foostruct.i=7;
strcpy(foostruct.str, "Test");

/* Copy the contents of the simplestruct instance to the
extendedstruct instance */
memcpy(&barstruct, &foostruct, sizeof(foostruct));

/* Populate remaining member of the extendedstruct instance */
barstruct.j=13;

/* Print values of members of the extendedstruct instance */
printf("i\t%d\nstr\t%s\nj\t%d\n", barstruct.i, barstruct.str,
barstruct.j);
}

Thanks in advance for any advice.




有可能sizeof foostruct == sizeof barstruct;

再举个例子:
< br $>
struct simplestruct

{

int i;

char str [7];

};


struct extendedstruct

{

int i;

char str [7] ;

unsigned char j;

};


典型的8bit char和32bit或16bit int和

最小填充,现在很可能sizeof foostruct == sizeof

barstruct,所以你在simplestruct中的填充会覆盖扩展结构中有用的

信息。 />
所以,不管你memcpy()(offsetof(struct simplestruct,str)+ sizeof

foostruct.str)bytes(来自< stddef.h>的offsetof)或

你关注在另一个回复中给出的提示,即

struct extendedstruct

{

struct simplestruct mySimple;

unsigned char j ;

}

然后,你甚至不需要memcpy():

barstruct.mySimple = foostruct;

足够了。

(另外,你可以滥用barstruct的地址作为

struct simplestruct *,如果有必要 - 但我没有说过;-))

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de address。



It is possible that sizeof foostruct == sizeof barstruct;
take another example:

struct simplestruct
{
int i;
char str[7];
};

struct extendedstruct
{
int i;
char str[7];
unsigned char j;
};

On typical implementations with 8bit char and 32bit or 16bit int and
minimal padding, it is now probable that sizeof foostruct == sizeof
barstruct, so your padding in simplestruct overwrites your useful
information in extendedstruct.
So, either you memcpy() (offsetof(struct simplestruct, str) + sizeof
foostruct.str) bytes (the offsetof macro comes from <stddef.h>) or
you follow the hint given in another reply, namely
struct extendedstruct
{
struct simplestruct mySimple;
unsigned char j;
}
Then, you do not even need memcpy():
barstruct.mySimple = foostruct;
suffices.
(In addition, you can abuse the address of barstruct as a
struct simplestruct*, if necessary -- but I have not said that ;-))
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


2005年7月27日12:32:08 -0700,他******** @ yahoo.com

< kg ******* @ pobox.com>写道:
On 27 Jul 2005 12:32:08 -0700, "he********@yahoo.com"
<kg*******@pobox.com> wrote:
我想使用memcpy将一个结构的内容复制到另一个结构中
这是原始结构的超集(第二个结构有额外的
)最后的成员)。我写了一个小程序来测试这个,它似乎工作得很好,但有没有做过像
这样可能导致任何问题的情况?
I want to use memcpy to copy the contents of one struct to another
which is a superset of the original struct (the second struct has extra
members at the end). I wrote a small program to test this, and it
seems to work fine, but are there any cases where doing something like
this could cause any problems?



无法保证两个

结构的公共部分中的填充是相同的。较短的结构也可能在结尾处有填充,但在较长的结构中,可能有一个

实际成员在同一偏移处。

<<删除电子邮件的del>>



There is no guarantee that the padding in the common part of the two
structs is the same. It is also possible for the shorter struct to
have padding at the end but in the longer struct there could be an
actual member at the same offset.
<<Remove the del for email>>


这篇关于将结构复制到更大的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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