我可以通过复制它的内存克隆一个对象吗?(Can I clone an object by copying its memory?)
我需要在我的控制下为有限数量的类撤销+重做堆栈,这个类必须非常非常快,并且使用RTTI和XML或流是不可行的,因为嵌套对象中的实例数可能高达2000+名单。 对象需要通过备忘录模式复制进出,并立即重新加载。
有没有办法通过复制内存并重新实例化内存中的对象来克隆对象?
I need to have undo+redo stack for a limited number of classes under my control that has to be very very very fast and using RTTI and XML or streams is not feasible as the count of instances can be as high as 2000+ in nested object lists. The objects need to be copied into and out of via memento pattern and reloaded instantly.
Is there a way to clone objects by copying the memory and re-instantiating the objects from that memory?
原文:https://stackoverflow.com/questions/7798036
最满意答案
现在该程序打印
Haha. Input sth here:
Haha. Input sth here:
并等待我的输入)。 如果我删除了scanf语句,它不在这里。 为什么?由于标准( N1570 ..“几乎C11”)如此说,§5.1.2.3/ 6(强调我的):
对一致性实施的最低要求是:
[..]
- 交互设备的输入和输出动态应按照7.21.3的规定进行。 这些要求的意图是尽可能快地出现无缓冲或线路缓冲输出,以确保提示消息实际上出现在等待输入的程序之前 。
[..]
即使您的输出不包含换行符,并且发送到缓冲的
stdout
行,它也必须在您的程序被允许等待输入之前出现。 这是因为stdout
和stdin
连接到一个终端,因此( 注意:这是实现定义的! )标准称之为“交互式设备”。Now the program prints
Haha. Input sth here:
(and wait for my input). It is not here if I remove the scanf statement. Why is that so?Because the standard (N1570 .. "almost C11") says so, §5.1.2.3/6 (emphasis mine):
The least requirements on a conforming implementation are:
[..]
- The input and output dynamics of interactive devices shall take place as specified in 7.21.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.
[..]
Even though your output does not contain a newline and is send to a line buffered
stdout
, it has to appear before your program is allowed to wait for input. This is becausestdout
andstdin
are connected to a terminal and thus are (Attention: This is implementation defined!) what the standard calls "interactive devices".
相关问答
更多-
现在该程序打印Haha. Input sth here: Haha. Input sth here:并等待我的输入)。 如果我删除了scanf语句,它不在这里。 为什么? 由于标准( N1570 ..“几乎C11”)如此说,§5.1.2.3/ 6(强调我的): 对一致性实施的最低要求是: [..] 交互设备的输入和输出动态应按照7.21.3的规定进行。 这些要求的意图是尽可能快地出现无缓冲或线路缓冲输出,以确保提示消息实际上出现在等待输入的程序之前 。 [..] 即使您的输出不包含换行符,并且发送到缓冲的s ...
-
我可以将python中的stdout重定向到某种字符串缓冲区吗?(Can I redirect the stdout in python into some sort of string buffer?)[2021-10-21]
from cStringIO import StringIO import sys old_stdout = sys.stdout sys.stdout = mystdout = StringIO() # blah blah lots of code ... sys.stdout = old_stdout # examine mystdout.getvalue() from cStringIO import StringIO # Python3 use: from io import String ... -
您只需写入os.Stdout文件即可完成此操作: var buff [1]byte for i, i < size; i++ { b = a[i] + i * 10 buff[0] = b os.Stdout.Write(buff[:]) } buff[0] = '\n' os.Stdout.Write(buff[:]) You can do this by simply writing to the os.Stdout file: var buff [1]byte for i, ...
-
I / O流stdout , stdin , stderr都被缓冲。 因此,直到以下任一情况都不会输出:1)输出缓冲区溢出2)输出换行符'\ n'3)针对特定输出流调用fflush。 4)执行输入语句或5)程序退出。 如果你想避免输出额外的换行符,那么建议使用: fflush( stdout ); the I/O streams stdout, stdin, stderr are all buffered. So nothing will be output until either: 1) the ou ...
-
你的程序的问题是, ctime返回时间字符串'\n' 。 你可以看到虚假的新行总是在打印时间之后。 将它复制到指定的缓冲区后,可以将其删除。 例如之后 strcpy(new.date_creation, ctime(&curtime)); 你可以加 new.date_creation[strlen(new.date_creation)-1] = '\0'; 您需要将此添加到您从ctime创建的每个字符串(或asctime ,如果将来使用该字符串)。 实际上,这个'\n'的存在是由C标准保证的。 您可以查 ...
-
缓冲区处理stdout(Buffer handling for stdout)[2023-07-18]
搞乱关闭和打开标准输出,“复制”,写入文件和从文件和其他奇怪的解决方案读取后,我发现一个符合我的需求,但不是那么便携,但不需要打开另一个文件: 如何在内存中缓冲stdout并从专用线程写入 #include#include #include #define MAX_LEN 40 int main( int argc, char *argv[] ) { char buffer[MAX_LEN+1] = {0}; int out_pi ... -
输出缓冲是一种优化技术。 将数据写入某些设备(硬盘fe)是一项昂贵的操作; 这就是缓冲出现的原因。 从本质上讲,它避免了逐字节(或char-by-char)写入数据并将其收集在缓冲区中,以便一次写入几个KiB数据。 作为优化,输出缓冲必须对用户透明(即使对程序也是透明的)。 它不得影响程序的行为; 无论是否缓冲(或使用不同大小的缓冲区),程序必须表现相同。 这就是你提到的规则。 缓冲区只是存储器中的一个区域,临时存储要写入的数据,直到有足够的数据累积,以使设备的实际写入过程高效。 某些设备(硬盘等)甚至不允 ...
-
找到在stdout上打印一些调试内容的源代码行(Find source code line which prints some debugging stuff on stdout)[2022-07-10]
您可以找到相关的源代码行引发异常,如果此行像这样发出: class WrapperOfStdout(): def __init__(self, old_stdout): self.old_stdout=old_stdout def write(self, data): assert not '{}' in data, data return self.old_stdout.write(data) 将此包装器添加到脚本中,并尽早包装stdou ... -
我确信tcl有很多优化,我建议你也优化代码; 那么你的方法是可行的。 检查你经常使用strlen ,每次遍历所有(很多)字符来计算长度 - 使用有关其长度的信息,例如维护char *outLineBufPtr 。 还可以使用strcat将\ n附加到outLineBuffer而不是使用昂贵的vsnprintf函数,或者只是手动复制char,如*outLineBufPtr++ ='\n'; 。 要实现像您这样的更高级别的概念,您必须开始考虑机器周期,以便更高级别的概念不会变得“昂贵”。 I am sure t ...
-
它是如何确定的? 它由格式字符串本身决定。 scanf函数将读取项目,直到它们不再与给定的格式说明符匹配。 然后它停止,让第一个“不合规”字符仍在缓冲区中。 如果你的意思是“如何在封面下处理?”,那就是另一个问题。 我对此的第一反应是“无所谓”。 ISO标准规定了语言的工作方式,并描述了能够做到这一点的“虚拟机”。 如果您遵守机器的规则,您无需担心事情如何发生。 我的第二个答案可能更令人满意,但非常依赖于实现。 为了提高效率,底层软件可能不会向实现提供任何数据,直到它有一个完整的行(尽管这当然很可能是可配置 ...