1

So I was looking at LLVM's Call instruction code, and it has a public constructor like so:

  static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
                          const Twine &NameStr,
                          Instruction *InsertBefore = nullptr) {
    return new (ComputeNumOperands(Args.size()))
        CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
  }

Create calls the appropriate private consstructor CallInst, but with an integer which I don't understand.

This is the code for ComputeNumOperands, and you can see that it returns int.

  /// Compute the number of operands to allocate.
  static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
    // We need one operand for the called function, plus the input operand
    // counts provided.
    return 1 + NumArgs + NumBundleInputs;
  }

Hence my question: what does new int Constructor() mean?

youneverknow
  • 137
  • 2
  • 8
  • 1
    https://en.cppreference.com/w/cpp/language/new#Placement_new – Hans Passant Sep 19 '19 at 22:21
  • If I understand it correctly, it allocates the object in the space pointed to by the `int`. However, I cannot reconcile that knowledge with this use case. In this case, the `int` is `Number of arguments + 1`, which could point to anything! – youneverknow Sep 19 '19 at 22:26
  • 1
    @Advait — something’s wrong here. That argument should be a pointer, not an `int`. The pointer points at the location where the object should be constructed. – Pete Becker Sep 19 '19 at 22:32
  • 1
    Beware the C++17 shenanigans, it made placement new a lot more versatile. Mostly to pass alignment requirements to the allocator, solving a long-standing fugly problem, but anything goes. You probably have to find that allocator to get ahead. – Hans Passant Sep 19 '19 at 22:35
  • 1
    Not a great duplicate, is it? 08 < 17. – Hans Passant Sep 19 '19 at 22:47

0 Answers0