3

In the disassembler output (ARM) I see:

    BLX _Znwj

where BLX is ARM procedure call and _Znwj is demangled as

$ c++filt  _Znwj
operator new(unsigned int)

What does that operator new do? Is it new or new[]? What type does it return? How do I explicitly call it from C++, something in the operator+(a,b) style?

(I know that it is possible to define operator new in a class, but this looks a bit different.)

2 Answers 2

3

A runtime function does not necessarily convert to a C++ function. For example, on a processor that has no floating point hardware, a simple statement like

a=b+c;

with a, b and c being floats, will probably be converted to a function call that takes b and c as parameters and returns the results. The user is not supposed to call that function directly.

In your case, consider that every new probably maps to something that allocates memory and calls a constructor function, and every new[] allocates memory for several elements and calls a constructor function on each element. The new _Znwj function is the memory allocator used internally by the new operator (at least if you're using gcc, which I assume), and it's not intended to be called directly from C++.

This program:

    class Foo {
            int bar;
    };

    int main(void) {
            Foo *baz=new Foo();
            Foo *var=new Foo[20];
    }

Compiles to (try gcc -S):

    stmfd   sp!, {fp, lr}
    .save {fp, lr}
    .setfp fp, sp, #4
    add     fp, sp, #4
    .pad #8
    sub     sp, sp, #8
    mov     r0, #4                <-- 4 bytes for single variable
    bl      _Znwj
    mov     r3, r0                <-- r0 returns the pointer to the allocated memory
    mov     r2, #0
    str     r2, [r3]
    str     r3, [fp, #-8]
    mov     r0, #80               <-- 80 bytes (20*4) for array
    bl      _Znaj
    mov     r3, r0                <-- r0 returns the pointer to the allocated memory
    str     r3, [fp, #-12]
    mov     r3, #0
    mov     r0, r3
    sub     sp, fp, #4
    @ sp needed
    ldmfd   sp!, {fp, lr}
    bx      lr

So, you see that the _Znwj resp. _Znaj are the allocator functions for a single class resp. an array of classes, both of them take the number of bytes required as parameter, and both of them return a pointer to the allocated memory as result.

To answer your questions:

  1. What does that operator new do ?

    It allocates memory for a new class instance.

  2. Is it new or new[] ?

    _Znwj is new, and _Znaj is new[].

  3. What type does it return ?

    It returns the address of the newly allocated memory block. In C++, the closest match to resemble that would be a void *, but note it's supposed to point to a class structure.

  4. How do I explicitly call it from C++, something in the operator+(a,b) style ?

    You're not supposed to do that. You use new or new[]. I assume the makers of gcc explicitly used a function name that cannot, normally, be called from a C++ program without a syntax error due to the name conflict.

1
  • In fact, void*x=operator new(4); compiles and generates the same code as int*x=new(int); Commented May 22, 2014 at 8:57
1

This is called "placement new" and was introduced in C++03. The way it works is that one can have, say a C++ class around a set of hardware registers and then use placement new to put the class exactly atop those hardware registers. For example, if a hardware-based hardware Random Number Generator (RNG) is defined using 32-bit Control Register, Status Register and Data Register all starting at offset 0x50000600, you could define a class:

class RNG_class
{
private:
  volatile uint32_t CR;  // set to 1 to enable
  volatile uint32_t SR;  // low bit set when next num available
  volatile uint32_t DR:
public;
  RNG_class() : CR(1) {} 
  uint32_t next() { while (SR&1 == 0); return DR; }
};

Elsewhere, then one can instantiate this class as

RNG_class RNG* = new(0x50000600) RNG_class();

And one can use it as:

r = RNG->next();

See this question for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.