Allocate memory for a class


When I allocate memory for a structure, in _Znwm (operator new) function I need to specify size. How can I determine this information dynamically?

; ModuleID = ‘/tmp/webcompile/_12211_0.bc’
target datalayout = “e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128”
target triple = “x86_64-unknown-linux-gnu”

%class.A = type { i32, float }

define i32 @main() uwtable {
%a = alloca %class.A*, align 8
%call = call noalias i8* @_Znwm(i64 8) ; << how can I determine this value?
%0 = bitcast i8* %call to %class.A*
store %class.A* %0, %class.A** %a, align 8
ret i32 0

declare noalias i8* @_Znwm(i64)


The value is statically known for a specific target, I believe.
In the example, the value is 8 bytes, encoded in the call instruction.

Actually, you don't even need that. The "1" means "one full struct"
rather than "after field 1" or anything similar. So it won't change
depending on how many fields are in the struct.

You might find this page from the docs useful:


Thank you! It works like a charm


No worries. I've just noticed a function ConstantExpr::getSizeOf which
looks like it automates the process if you want to simplify your C++