English 中文(简体)
书写非阶级的记忆
原标题:Writing non-class types onto raw memory
  • 时间:2010-08-01 06:55:39
  •  标签:
  • c++

鉴于原始记忆的“博彩”是个空白点,因此,有两种方式撰写材料。

首先是使用新安置办法。 这种方法的好处是,当我们处理阶级类型时,就能够把机器人称为汽车。 然而,当我处理非阶级类型时,更不用说 cast? 我认为,这样做可能更快。

(pLocation is a void pointer to a blob of memory)

// ----- Is this better -----
*reinterpret_cast<char*>(pLocation) = pattern;

// ----- Or is this better -----
::new(pLocation) char(pattern);
最佳回答

我利用以下方案审视了这些技术的生成组合:

#include <new>

char blob[128];

int main() {
    void *pLocation = blob;
    char pattern =  x ;
#ifdef CAST
    *reinterpret_cast<char*>(pLocation) = pattern;
#else
    ::new(pLocation) char(pattern);
#endif
}

I m 采用4.4.3g++,在有违约汇编旗的六氯环己烷64轨道上使用。

The relevant part of the asm for placement new:

    movb    $120, -1(%rbp)
    movq    -16(%rbp), %rax
    movq    %rax, %rsi
    movl    $1, %edi
    call    _ZnwmPv
    movq    %rax, %rdx
    testq   %rdx, %rdx
    je  .L5
    movzbl  -1(%rbp), %edx
    movb    %dl, (%rax)
.L5:

从我收集的情况来看,这实际上称作安置新经营者,并检查其返回价值,尽管它总是成功。 然后,将<代码>x的数值写入交还的记忆中。

<<><>>t>reinterpret_cast:

    movb    $120, -1(%rbp)
    movq    -16(%rbp), %rax
    movzbl  -1(%rbp), %edx
    movb    %dl, (%rax)

请注意,这些指示与前两个和最后两个位置:<编码>新

使用<代码>-O1,两种代码生成i Equal。 集会:

movb    $120, blob(%rip)

因此,如果你重新担心业绩,就没有。 任何其他电解机也可能将两者都减至相同的代码。

问题回答

虽然将原始记忆注入物体在实践中可能发挥作用,但它正式援引未经界定的行为,因此根据C++标准,你的代码可能做任何事情。

Placement new, OTOH, is a technique to invoke a constructor at a particular address, and construction is what officially turns raw memory into valid objects. That s why I would prefer placement new.
Just to make sure, I would also have the destructor for such objects is called. While you say that you only need this for PODs and PODs destruction is a no-op, many bugs I have seen in my carrier were in code that was written with a set of restrictions in mind, but had later some of the restrictions lifted and suddenly found itself in an environment with which it was unable to cope.

还指出,可能有一些平台,因为并非所有可能的比照模式都是有效的价值观,哪怕是内在的。 这种平台还可能妨碍人们获得这种模式的价值。 例如,全方位轨道型号不能成为浮动型的有效数值,因此即使事先使记忆变得零也不能防止硬件例外。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签