好的是移动一个“对象”。会愿意不愿意? [英] Okay to move an "object" will-nilly?

查看:85
本文介绍了好的是移动一个“对象”。会愿意不愿意?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我的英语方言似乎有时会让人感到困惑,所以我首先要说清楚我在下面的文章中使用的几个术语:)


(1)国内,我的意思是普通的,普通的,不是

特殊或奇怪的。


(2)willy-nilly,我的意思是

haphazardly,但没有任何鲁莽感(即兴高采烈地

做某事而不期待任何负面影响,即使实际上可能会产生负面影响,例如
),例如一个小孩会喝酒

a一杯液体留在厨房的桌子上,幸福地说,

无知是否含有漂白剂。


============================================ ====== ====


好​​吧我会尽量保持C ++部分的简洁(是的,我的最终问题

是关于C):


在C ++中,有些东西叫做对象。它们的行为就像

变量,但是当它们被创建,销毁,

复制,分配给它们等时会发生特殊事情。(具体来说,程序员可以指定代码
要执行的



如果你想用C ++复制一个对象,那么简单的做法是不明智的:


ArbitraryClass original_object;


ArbitraryClass * p = malloc(sizeof * p);


memcpy(p ,& original_object,sizeof original_object);

之所以建议使用对象,可以通过 ;构造"这可能会导致获得动态分配内存等资源的b $ b。如果我们执行

浅拷贝通过使用memcpy,然后生成的副本使用

与原始资源相同的资源(实际上,我们希望它获得其拥有的b $ b b)。通过使用复制构造函数来纠正这个问题...但我不会在这里进入



无论如何,尽管如此,我认为在内存中移动一个对象,即只需重新定位它就可以了./ $ $ $ $ $ C ++这将

涉及memcpy''将其字节移到另一个位置而根本不用

进一步使用原始文件。然而有人告诉我,这也是在C ++中被建议的,因为对象的地址可能对其内部工作有一定的影响(即特殊的)获得

的代码,当你分配给它,销毁它等等。一个主要的例子是

是一个包含缓冲区的对象,它还包含一个指针

到缓冲区中的当前位置,如:


struct StringStream {


char buffer [1024];


char * p_pos; / *指向缓冲区元素* /


};


如果我们要移动类型为StringStream的对象。 ,然后p_pos

实际上会腐败。


那么具有什么意义呢?


最近,我编写了一种用于排序数组的时髦算法(无论是
它是char',int',double'或者C ++类的数组类型),

和算法通过使用memcpy和

丢弃原始文件来无限地移动对象。但是,我的算法被破坏了。从某种意义上来说,它会腐蚀那些地址对他们内部工作有一定意义的对象。


因此,那里有是一种公认​​的C ++礼仪,在编写可重复使用的代码(例如排序算法)时,你不要只是重新定位对象,而不是
nilly,但是你必须采取更加光荣的手段。


我的问题是:

C中是否存在任何此类礼仪?如果我用C编写一个国内的

排序算法,我是否可以接受重新定位

的对象呢?

-


Frederick Gotham

(My dialect of English seems to puzzle people at times, so I''ll first
clarify a few terms which I use in the following post:)

(1) By "domestic", I mean "ordinary, run-of-the-mill, not
extraordinary or strange".

(2) By "willy-nilly", I mean something along the lines of
"haphazardly", but without any sense of recklessness (i.e. gleefully
doing something without expecting any sort of negative effect, even
though there may in fact be a negative effect), e.g. "A child would drink
a glass of liquid left on the kitchen table willy-nilly, blissfully
ignorant as to whether it contained bleach".

================================================== ====

Okay I''ll try to keep the C++ part brief (and yes, my eventual question
is about C):

In C++, there are things called objects. They behave just like
variables, but special things happen when they''re created, destroyed,
copied, assigned to, etc.. (specifically, the programmer can specify code
which is to be executed).

If you want to copy an object in C++, is it ill-advised to simply do:

ArbitraryClass original_object;

ArbitraryClass *p = malloc( sizeof *p );

memcpy( p, &original_object, sizeof original_object );
The reason why it''s il-advised is that when an object is created, code
can be executed via a "constructor" which may result in the acquisition
of resources such as dynamically allocated memory. If we perform a
"shallow copy" by using "memcpy", then the resulting copy is using the
same resources as the original (when in reality, we want it to get its
own). The matter is remedied by using a copy-constructor... but I won''t
get into that here.

Anyway, notwithstanding any of that, I thought it would still be OK in
C++ to move an object in memory, i.e. simply re-locate it. This would
involve memcpy''ing its bytes to a different location and simply not
making any further use of the original. I was told however that this also
is il-advised in C++, because the object''s address may be of some
significance to its internal workings (i.e. the special code that gets
executed when you assign to it, destroy it, etc.). A prime example would
be an object which contains a buffer, and which also contains a pointer
to the current position in the buffer, something like:

struct StringStream {

char buffer[1024];

char *p_pos; /* Points to an element of buffer */

};

If we were to move an object of the type "StringStream", then "p_pos"
would effectively become corrupt.

So what''s the significance of this?

Recently, I wrote a funky kind of algorithm for sorting an array (whether
it be an array of char''s, int''s, double''s or perhaps a C++ class type),
and the algorithm moved objects around willy-nilly by using memcpy and
discarding the original. However, my algorithm was "broken" in the sense
that it would corrupt objects whose address was of some significance to
their internal workings.

Hence, there is an accepted etiquette in C++ that, when writing re-usable
code such as a sorting algorithm, you DON''T just re-locate objects willy-
nilly, but rather you''ve to resort to more elustrious means.

My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?
--

Frederick Gotham

推荐答案

Frederick Gotham写道:

< snip>
Frederick Gotham wrote:
<snip>
我的问题是:
C中是否存在任何此类礼仪?如果我用C编写一个国内的排序算法,我是否可以接受重新定位
对象?
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?




如果没有任何东西依赖于先前建立的数组的订单,那么为什么不呢?


虽然我应该添加它,即使在C ++中如果你有一个类

实例的数组,如果你超载比较和

赋值运算符,你可以对它们进行排序。


Tom



Provided nothing was relying on the previously established order of the
the array, yeah sure why not?

Though I should add that even in C++ if you had an array of class
instances you could sort them if you had overloaded the comparison and
assignment operators.

Tom


Frederick Gotham写道:
Frederick Gotham wrote:

在C ++中,有一些叫做对象的东西。


在C

int int_object;

是一个名为int_object的对象的声明。

我相信'在C ++中也是如此。

我被告知,这也是在C ++中被建议的,因为对象的地址可能是某些
对其内部工作的重要性(即分配给它,销毁它等时执行的特殊代码)。


文件类型对象与C中的类似。

我的问题是:
C中是否存在任何此类礼仪?如果我用C编写一个国内的排序算法,我是否可以接受重新定位
对象?
In C++, there are things called objects.
In C
int int_object;
is a declaration of an object named int_object.
I believe that''s also true in C++.
I was told however that this also
is il-advised in C++, because the object''s address may be of some
significance to its internal workings (i.e. the special code that gets
executed when you assign to it, destroy it, etc.).
FILE type objects are like that in C.
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?




当复制或覆盖对象时出现问题,

,例如FILE类型对象,

和字符串文字引用的对象,

你最好对一组指向对象的指针进行排序。


-

pete



When there''s a problem with copying or overwriting objects,
such as with FILE type objects,
and the objects refered to by string literals,
you would be better off sorting an array of pointers to the objects.

--
pete


Frederick Gotham写道:
Frederick Gotham wrote:
...
struct StringStream {
char buffer [1024];
char * p_pos; / *指向缓冲区元素* /
};

如果我们要移动类型为StringStream的对象,那么p_pos
将有效腐败。
...
我的问题是:
C中是否存在这样的礼仪?如果我用C编写一个国内的排序算法,我是否可以接受重新定位
对象?
...
struct StringStream {
char buffer[1024];
char *p_pos; /* Points to an element of buffer */
};

If we were to move an object of the type "StringStream", then "p_pos"
would effectively become corrupt.
...
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?




取决于对象及其用法。一般来说,答案是否定的。


不仅对象可能包含指向

重定位的指针,(如在你自己的StringStream示例中) ,)他们的地址

也可以存储在代码中其他地方的指针中,那些

将被无效。


一更多问题:如何通过调用

malloc()或者因为对象被声明为自动变量来处理动态分配的内存中的对象重定位如何处理?b $ b

(对于静态和动态物体的混合也是如此。)



Depends on the objects and their usage. In general, the answer is no.

Not only the objects may contain pointers that would be invalidated by
the relocation, (as in your own StringStream example,) their addresses
could also be stored in pointers somewhere else in the code and those
would be invalidated.

One more issue: How would you handle the relocation of objects
residing in dynamically allocated memory, either explicitly by calling
malloc() or because the objects were declared as automatic variables?
(The same for mixture of static and dynamic objects.)


这篇关于好的是移动一个“对象”。会愿意不愿意?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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