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:
What does that operator new
do ?
It allocates memory for a new class instance.
Is it new
or new[]
?
_Znwj
is new
, and _Znaj
is new[]
.
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.
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.