且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

好的是移动一个“对象”。会愿意不愿意?

更新时间:2022-05-19 17:03:10

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.)