gpt4 book ai didi

c++ - placement new 是如何运作的?

转载 作者:行者123 更新时间:2023-11-28 00:20:46 27 4
gpt4 key购买 nike

<分区>

我写了下面的想法,认为它应该在运行时出现段错误。然而它没有,它运行良好,我不明白为什么。

#include <cstdlib>
#include <cstdio>
#include <new>

struct MyStruct
{
double *a;

MyStruct()
: a(NULL)
{ printf("Default constructor\n"); }
MyStruct( double *b )
: a(b)
{}
MyStruct( const MyStruct& other )
{
printf("Copy-constructor\n");
if ( a != NULL && *a != 3.14 )
a = other.a;
}
};

int main()
{
double num = 3.14;
MyStruct obj( &num );

void *ptr = ::operator new( sizeof(MyStruct) );
new (ptr) MyStruct(obj);

delete (MyStruct*) ptr; // Calls ~MyStruct
}

输出是:

Copy-constructor

当我写 void *ptr =::operator new( sizeof(MyStruct) ); 我知道这只分配内存,不应该调用默认构造函数。它似乎并没有:很好。

当我编写 new (ptr) MyStruct(obj); 时,如果它像我想象的那样工作,我希望它会出现段错误。我认为这等同于 ( (MyStruct*) ptr )->MyStruct(obj)。如果是这样,行 if ( a != NULL && *a != 3.14 ) 应该到达 *a != 3.14 和段错误,因为 a 尚未初始化。

我的问题是 a 似乎没有被初始化(因为没有输出“Default constructor”),但是之前的仍然没有段错误。我错过了什么?


下面是生成的汇编代码(不知道怎么读):

    .file   "placement_new.cpp"
.section .rodata.cst8,"aM",@progbits,8
.align 8
.LCPI0_0:
.quad 4614253070214989087 # double 3.1400000000000001
.text
.globl main
.align 16, 0x90
.type main,@function
main: # @main
.cfi_startproc
# BB#0:
push rbp
.Ltmp2:
.cfi_def_cfa_offset 16
.Ltmp3:
.cfi_offset rbp, -16
mov rbp, rsp
.Ltmp4:
.cfi_def_cfa_register rbp
sub rsp, 48
lea rdi, qword ptr [rbp - 24]
lea rsi, qword ptr [rbp - 16]
movsd xmm0, qword ptr [.LCPI0_0]
mov dword ptr [rbp - 4], 0
movsd qword ptr [rbp - 16], xmm0
call _ZN8MyStructC2EPd
movabs rdi, 8
call _Znwm
mov qword ptr [rbp - 32], rax
mov rax, qword ptr [rbp - 32]
cmp rax, 0
mov qword ptr [rbp - 40], rax # 8-byte Spill
je .LBB0_2
# BB#1:
lea rsi, qword ptr [rbp - 24]
mov rax, qword ptr [rbp - 40] # 8-byte Reload
mov rdi, rax
call _ZN8MyStructC2ERKS_
.LBB0_2:
mov rax, qword ptr [rbp - 32]
cmp rax, 0
mov qword ptr [rbp - 48], rax # 8-byte Spill
je .LBB0_4
# BB#3:
mov rax, qword ptr [rbp - 48] # 8-byte Reload
mov rdi, rax
call _ZdlPv
.LBB0_4:
mov eax, dword ptr [rbp - 4]
add rsp, 48
pop rbp
ret
.Ltmp5:
.size main, .Ltmp5-main
.cfi_endproc

.section .text._ZN8MyStructC2EPd,"axG",@progbits,_ZN8MyStructC2EPd,comdat
.weak _ZN8MyStructC2EPd
.align 16, 0x90
.type _ZN8MyStructC2EPd,@function
_ZN8MyStructC2EPd: # @_ZN8MyStructC2EPd
.cfi_startproc
# BB#0:
push rbp
.Ltmp8:
.cfi_def_cfa_offset 16
.Ltmp9:
.cfi_offset rbp, -16
mov rbp, rsp
.Ltmp10:
.cfi_def_cfa_register rbp
mov qword ptr [rbp - 8], rdi
mov qword ptr [rbp - 16], rsi
mov rsi, qword ptr [rbp - 8]
mov rdi, qword ptr [rbp - 16]
mov qword ptr [rsi], rdi
pop rbp
ret
.Ltmp11:
.size _ZN8MyStructC2EPd, .Ltmp11-_ZN8MyStructC2EPd
.cfi_endproc

.section .rodata.cst8,"aM",@progbits,8
.align 8
.LCPI2_0:
.quad 4614253070214989087 # double 3.1400000000000001
.section .text._ZN8MyStructC2ERKS_,"axG",@progbits,_ZN8MyStructC2ERKS_,comdat
.weak _ZN8MyStructC2ERKS_
.align 16, 0x90
.type _ZN8MyStructC2ERKS_,@function
_ZN8MyStructC2ERKS_: # @_ZN8MyStructC2ERKS_
.cfi_startproc
# BB#0:
push rbp
.Ltmp14:
.cfi_def_cfa_offset 16
.Ltmp15:
.cfi_offset rbp, -16
mov rbp, rsp
.Ltmp16:
.cfi_def_cfa_register rbp
sub rsp, 32
lea rax, qword ptr [.L.str]
mov qword ptr [rbp - 8], rdi
mov qword ptr [rbp - 16], rsi
mov rsi, qword ptr [rbp - 8]
mov rdi, rax
mov al, 0
mov qword ptr [rbp - 24], rsi # 8-byte Spill
call printf
mov rsi, qword ptr [rbp - 24] # 8-byte Reload
cmp qword ptr [rsi], 0
mov dword ptr [rbp - 28], eax # 4-byte Spill
je .LBB2_3
# BB#1:
movsd xmm0, qword ptr [.LCPI2_0]
mov rax, qword ptr [rbp - 24] # 8-byte Reload
mov rcx, qword ptr [rax]
movsd xmm1, qword ptr [rcx]
ucomisd xmm1, xmm0
jne .LBB2_2
jp .LBB2_2
jmp .LBB2_3
.LBB2_2:
mov rax, qword ptr [rbp - 16]
mov rax, qword ptr [rax]
mov rcx, qword ptr [rbp - 24] # 8-byte Reload
mov qword ptr [rcx], rax
.LBB2_3:
add rsp, 32
pop rbp
ret
.Ltmp17:
.size _ZN8MyStructC2ERKS_, .Ltmp17-_ZN8MyStructC2ERKS_
.cfi_endproc

.type .L.str,@object # @.str
.section .rodata.str1.1,"aMS",@progbits,1
.L.str:
.asciz "Copy-constructor\n"
.size .L.str, 18


.ident "Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)"
.section ".note.GNU-stack","",@progbits

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com