使用 分配堆栈空间时alloca()
,是否需要清除内存或保证只包含零?
我想出了以下 LLVM 代码。尽管它可以编译,但它会导致核心转储。
在代码中,我试图实现对象的实例化,然后将其分配给“成员变量”。
%Main = type { %A*, i32 }
%A = type { %String, %String }
%String = type { i32, i8* }
define i32 @main() {
body:
%Main = alloca %Main
%0 = bitcast %Main* %Main to i8*
call void @llvm.memset.p0i8.i32(i8* %0, i8 0, i32 4, i32 4, i1 false)
call void @test(%Main* %Main)
ret i32 0
}
define void @test(%Main* %self) {
body:
%0 = load %Main* %self
%1 = extractvalue %Main %0, 0
; Overwrite the object A in Main by a new instance.
%A = alloca %A
; Set to zero.
%2 = bitcast %A* %A to i8*
call void @llvm.memset.p0i8.i32(i8* %2, i8 0, i32 4, i32 4, i1 false)
; ... execute the constructor of A ...
; Set the new instance in Main.
%3 = load %A* %A
store %A %3, %A* %1
br label %return
return: ; preds = %body
ret void
}
; Function Attrs: nounwind
declare void @llvm.memset.p0i8.i32(i8* nocapture, i8, i32, i32, i1) #0
declare i32 @printf(i8*, ...)