结构分配 [英] Struct assignment

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

问题描述

如果我有ff结构:


struct A

{

unsigned int i;

char s [LONG_ENOUGH];

} a,b;

并在这样的代码中使用它们:


ai = 42;

strcpy(as,test);


bi = 100;


b = a;


此时,a的(按位?)副本是b。问题是:


1)。是b.s现在ptr到a.s? (我想是的)

如果是这样,如果例如变量''a'超出范围(?)


2)会发生什么。编译器是否生成隐含的memcpy。或memmove或

幕后它看到这样的作业(为了避免悬空ptrs)?

If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"test");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b. Question is:

1). is b.s now ptr to a.s ? (I think so)
If so, what happens if for instance variable ''a'' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?

推荐答案



灰色外星人 < gr ** @ andromeda.com写信息

新闻:呃********************* @ bt.com ...

"Grey Alien" <gr**@andromeda.comwrote in message
news:er*********************@bt.com...

如果我有ff结构:


struct A

{

unsigned int i;

char s [LONG_ENOUGH];

} a,b;


并使用它们在这样的代码中:


ai = 42;

strcpy(as,test);


bi = 100;


b = a;


此时,a的(按位?)副本是b。问题是:


1)。是b.s现在ptr到a.s? (我想是的)

如果是这样,如果例如变量''a'超出范围(?)


2)会发生什么。编译器是否生成隐含的memcpy。或memmove或

幕后它看到这样的作业(为了避免悬空ptrs)?
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"test");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b. Question is:

1). is b.s now ptr to a.s ? (I think so)
If so, what happens if for instance variable ''a'' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?



答案是2.结构分配将导致对memcpy的调用是

,或者如果结构足够小就会发出等效代码,这会造成浪费,并且编译器很聪明。

但是如果结构包含指针,那么指针的值将被覆盖为
。所以你必须非常小心,不要孤儿回忆或

用别名创造奇怪的错误。


-

免费游戏和编程好东西。
http://www.personal.leeds .ac.uk / ~bgy1mm

The answer is 2. A structure assignment will cause a call to memcpy to be
made, or equivalent code emitted if the structure is small enough to make
this wasteful and the compiler is clever.
However if structures contain pointers then the values of the pointers are
overwritten. So you have to be extremely careful not to orphan memory or
create peculiar bugs with aliases.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


6月30日上午9:43,Gray Alien< g ... @ andromeda。 comwrote:
On Jun 30, 9:43 am, Grey Alien <g...@andromeda.comwrote:

如果我有ff结构:


struct A

{

unsigned int i;

char s [LONG_ENOUGH];


} a,b;


并在这样的代码中使用它们:


ai = 42;

strcpy(as,test);


bi = 100;


b = a;


此时,a(按位?)副本是b。

问题是:


1)。是b.s现在ptr到a.s? (我想是这样)
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];

} a, b;

And use them in code like this:

a.i = 42 ;
strcpy(a.s,"test");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b.
Question is:

1). is b.s now ptr to a.s ? (I think so)



我认为你混淆了数组和指针。由于是一个数组,

[0]到[LONG_ENOUGH-1]实际存储在结构中,

由作业复制。


如果只是指向其他地方分配的内存的指针,

赋值只会复制指针。

I think you are confusing arrays and pointers. Since a.s is an array,
a.s[0] to a.s[LONG_ENOUGH-1] are actually stored in the structure and
are copied by the assignment.

If a.s were just a pointer to memory allocated elsewhere the
assignment would just copy the pointer.


2)。编译器是否生成隐含的memcpy

或memmove。在幕后看到这样的

作业(为了避免悬空ptrs)?
2). Does the compiler generate an implicit "memcpy"
or "memmove" behind the scenes when it sees an
assignment like this (to avoid dangling ptrs)?



有一个显式副本,因为a.s是一个数组。如果

是一个指针,则不会有隐式副本,并且

可能会导致诸如悬空指针之类的坏事。


-thomas

There is an explicit copy since a.s is an array. If a.s
were a pointer there would not be an implicit copy, and
Bad Things such as dangling pointers could result.

-thomas


Gray Alien写道,在30/06/07 17:43:
Grey Alien wrote, On 30/06/07 17:43:

如果我有ff结构:


struct A

{

unsigned int i;

char s [LONG_ENOUGH];

} a,b;


并在以下代码中使用它们:


ai = 42;

strcpy(as,test);


bi = 100;


b = a;


此时,a的(按位?)副本是b。
If I have the ff struct:

struct A
{
unsigned int i;
char s[LONG_ENOUGH];
} a, b;
And use them in code like this:

a.i = 42 ;
strcpy(a.s,"test");

b.i = 100 ;

b = a ;

at this point, a (bitwise?) copy of a is made to b.



它不是按位复制,它是结构中所有元素的副本。

It is not a bitwise copy, it is a copy of all the elements in the struct.


问题是:


1)。是b.s现在ptr到a.s? (我想是这样)
Question is:

1). is b.s now ptr to a.s ? (I think so)



由于bs没有被定义为指针,你认为

赋值可以神奇地将它变成一个数组是否为
a指针?您需要阅读comp.lang.c常见问题解答第6部分
http:// c-faq.com/ 特别是关于是否
指针和数组是否相同的问题。

Since b.s was not defined as a pointer, what makes you think an
assignment could magically transform it from being an array in to being
a pointer? You need to read section 6 of the comp.lang.c FAQ at
http://c-faq.com/ specifically the questions dealing with whether
pointers and arrays are the same thing.


如果是这样的话,如果例如变量''a'超出范围(?)


2)会发生什么。编译器是否生成隐含的memcpy。或memmove或

幕后它看到这样的作业(避免悬空ptrs)?
If so, what happens if for instance variable ''a'' goes out of scope (?)

2). Does the compiler generate an implicit "memcpy" or "memmove" behind
the scenes when it sees an assignment like this (to avoid dangling ptrs)?



C很难做到背后的事情。如果在你的结构中有没有

指针(那么没有)那么在作业完成之后它们会指向与原始结构相同的位置,

当那个地方不再有效时(例如,自动退出

范围)指针不再有效。

- -

Flash Gordon

It is very rare for C to do things behind your back. Had there been
pointers in your struct (which there were not) then after the assignment
they would point to the same place as they point in the original struct,
and when that place is no longer valid (an automatic that goes out of
scope, for example) the pointers are no longer valid.
--
Flash Gordon


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

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