51

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

Or you to me want to tell, how the compiler interposes the machine code operating with registers (R/E) SP and (R/E) BP? It is not necessary!... Exactly too most function _malloca does.

Tell ell then why at/O2 it is more than code with alloca?
[spoiler the Code of the healthy person]

struct MyClass {int x; MyClass () {x = rand ();} int f () {return x;}};
int main ()
{
MyClass b;
return b.f ();
}
EXTRN rand:PROC
main PROC; COMDAT
jmp rand
main ENDP
MyClass:: f, COMDAT PROC
mov eax, DWORD PTR [rcx]
ret 0
MyClass:: f ENDP
MyClass:: MyClass, COMDAT PROC
push rbx
sub rsp, 32; 00000020H
mov rbx, rcx
call rand
mov DWORD PTR [rbx], eax
mov rax, rbx
add rsp, 32; 00000020H
pop rbx
ret 0
MyClass:: MyClass ENDP

[/spoiler]
Against
[spoiler the Code of the smoker]

struct MyClass {int x; MyClass () {x = rand ();} int f () {return x;}};
int main ()
{
MyClass* p = (MyClass *) alloca (sizeof (MyClass));
if (NULL! = p) new (p) MyClass ();
return p-> f ();
}
EXTRN rand:PROC
EXTRN __ GSHandlerCheck:PROC
EXTRN __ security_check_cookie:PROC
__ $ArrayPad $ = 0
main PROC; COMDAT
push rbp
sub rsp, 48; 00000030H
lea rbp, QWORD PTR [rsp+32]
mov QWORD PTR [rbp+32], rbx
mov rax, QWORD PTR __ security_cookie
xor rax, rbp
mov QWORD PTR __ $ArrayPad $ [rbp], rax
mov eax, DWORD PTR [rsp]
sub rsp, 16
lea rbx, QWORD PTR [rsp+32]
mov ecx, DWORD PTR [rbx]
test rbx, rbx
je SHORT $LN12@main
call rand
mov DWORD PTR [rbx], eax
$LN12@main:
mov eax, DWORD PTR [rbx]
mov rcx, QWORD PTR __ $ArrayPad $ [rbp]
xor rcx, rbp
call __ security_check_cookie
mov rbx, QWORD PTR [rbp+32]
lea rsp, QWORD PTR [rbp+16]
pop rbp
ret 0
main ENDP
MyClass:: f, COMDAT PROC
mov eax, DWORD PTR [rcx]
ret 0
MyClass:: f ENDP
MyClass:: MyClass, COMDAT PROC
push rbx
sub rsp, 32; 00000020H
mov rbx, rcx
call rand
mov DWORD PTR [rbx], eax
mov rax, rbx
add rsp, 32; 00000020H
pop rbx
ret 0
MyClass:: MyClass ENDP
void * __ ptr64 __ cdecl operator new (unsigned __ int64, void * __ ptr64) PROC; operator new, COMDAT
mov rax, rdx
ret 0
void * __ ptr64 __ cdecl operator new (unsigned __ int64, void * __ ptr64) ENDP; operator new

[/spoiler]

52

Re: Constant copy of a class. Who as does?

NekZ what, in this case the difference, is more than code either less or whence the code selecting storage on a stack - from a library function, an intrinsic function undertakes or, actually, is interposed by the compiler? It is a question of PRINCIPLES of layout and initialization of a copy of a class on a stack.  here your details which to these principles have no direct relation?

53

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

NekZ what, in this case the difference, is more than code either less or whence the code selecting storage on a stack - from a library function, an intrinsic function undertakes or, actually, is interposed by the compiler? It is a question of PRINCIPLES of layout and initialization of a copy of a class on a stack.  here your details which to these principles have no direct relation?

Precisely you could achieve that without everyones

char buf [sizeof (MyClass)];
MyClass* p = (MyClass *) buf;
new (p) MyClass ();

Surrenders to me, you simply want to argue.

54

Re: Constant copy of a class. Who as does?

NekZ wrote:

Precisely you could achieve that without everyones

char buf [sizeof (MyClass)];
MyClass* p = (MyClass *) buf;
new (p) MyClass ();

Surrenders to me, you simply want to argue.

Could! Also what further? Speech not about a method of selection of storage.

55

Re: Constant copy of a class. Who as does?

* There is a possibility to "tell" new const Type (), but there is no possibility this const though somehow to "hear" in implementation of the overloaded operator new, the designer or  a class;
Operator NEW selects storage under object. How you think, storages all the same, what object in it lies, changed or not?
All the same. Should operator new depend from  object under which storage is selected? No, should not.

wrote:

What for possibility to write const if at creation of a copy of a class in a heap this const, the equal account, on what does not influence?

You can anchor this object only to the constant link or the constant pointer, and the object after creation will not change.
* There is a possibility to redefine placement new, but the compiler does not feign its call at determination of a copy of object on a stack though, such behavior would be logical;
placement new is a method to cause  and to carry out initialization of new object in the given storage.
Object creation consists of two phases:
Storage under object somehow is selected
In this storage the new object by means of a call of the designer is initialized.
On a stack (auto storage) it happens so:
Storage under object by reservation of a place of certain volume in a stack is selected.
By means of a call of the designer the object there is initialized.
placement new does EXACTLY second point of this plan, i.e. at object creation on a stack just "it is emulated placement new" so that still is necessary to you from With ++, not clearly.
* we Can  templates with deduction on parameters of the designer, but we can not  templates with deduction on type and declaration qualifiers lvalue;
At creation of object and removal of object the object should be changed, in it an initialization essence.
The object at first is in a non-initialized state, the designer is caused, and object contents change in such a manner that the object is already in the initialized state. Key here - that changes. IT means that without looking at any qualifiers used at determination of new object, he is obliged to exchange. I.e. qualifiers should not and cannot work in the designer. The same it is possible to tell and about corrupting of object and .
Well and actually (if to think) qualifiers objects, and links to them (in any kind) possess not.

56

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
Thanks,  smile))

Not, well it truly painted all, it and becomes. It is possible even such hands to do.
If you only did not neigh that it did not finish thinking that it and is emulation placement new you neighed in vain.

57

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

On a stack (auto storage) it happens so:
Storage under object by reservation of a place of certain volume in a stack is selected.
By means of a call of the designer the object there is initialized.
placement new does EXACTLY second point of this plan, i.e. at object creation on a stack just "it is emulated placement new" so that still is necessary to you from With ++, not clearly.

In my understanding, "emulation placement new" it when the compiler creates the call code placement new irrespective of, whether implementation of this operator by the compiler by default (on mine, it does not form till now by default and the compiler initializes a copy on a stack simply kicking the designer) is created or the developer made this implementation independently.

58

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

Not, well it truly painted all, it and becomes. It is possible even such hands to do.
If you only did not neigh that it did not finish thinking that it and is emulation placement new you neighed in vain.

Well how to tell. I  counted as that is the given companion object creation on a stack with a direct call  and placement new. From the point of view of result of performance of the code, probably, yes, it is equivalent, but that happens under a cowl, absolutely another, as has been illustrated above under spoilers. Before a call call moves sp for the size of the frame downwards while the call  moves sp downwards already in the function, i.e. after call and performs a heap of unnecessary operation as the size is known at a compilation stage, therefore it is so much code and  the unnecessary.
On the same logic it is possible to tell in the same way that removal of tonsils through 0 and through a mouth - things equivalent.

59

Re: Constant copy of a class. Who as does?

NekZ wrote:

Well how to tell. I  counted as that is the given companion object creation on a stack with a direct call  and placement new. From the point of view of result of performance of the code, probably, yes, it is equivalent, but that happens under a cowl, absolutely another, as has been illustrated above under spoilers.

I not is worse you know that happens under a cowl. You here not one "expert" on the assembler and optimization of the machine code.

60

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

I not is worse you know that happens under a cowl. You here not one "expert" on the assembler and optimization of the machine code.

Unless you did not understand in what an essence?
In it.

rdb_dev wrote:

So why to the compiler not to create placement new by default if it is not redefined and not to feign its call? Would receive more controlled behavior.

Once again we look in the code of the healthy person and it is not visible there operator new, unlike the underlaying.  it is not necessary to break it into separate phases  storages on a stack and a call of the designer through placement new are superfluous calls. It does as it is possible more shortly and as he considers it necessary.
P.S. It is amusing that clang with this example consults better VS'.

61

Re: Constant copy of a class. Who as does?

NekZ wrote:

Kompiljatru it is not necessary to break it into separate phases  storages on a stack and a call of the designer through placement new are superfluous calls. It does as it is possible more shortly and as he considers it necessary.

That is the compiler does not form the code for the extension of a frame of a stack at the declaration there a class copy? It is done by the designer of object? smile))
Actually all of us equally have "separate phases" - separately the extension of a frame of a stack and separately a call of the designer, just as separate phases of the operator new - separately selection of storage from a heap and separately a call of the designer.

62

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

That is the compiler does not form the code for the extension of a frame of a stack at the declaration there a class copy? It is done by the designer of object? smile))

Forms, indeed.

rdb_dev wrote:

Actually all of us equally have "separate phases" - separately the extension of a frame of a stack and separately a call of the designer, just as separate phases of the operator new - separately selection of storage from a heap and separately a call of the designer.

And here not the fact, after all if you tried to look that do clang or gcc with the same code:

main:
jmp rand

Any calls of the operator new. Generally calls. Because be it as you want, we would lose many important , leading such  to the code. And the example with MSVC the compiler illustrates it visually.

63

Re: Constant copy of a class. Who as does?

NekZ, what difference how the call if the address,  in a stack extreme before control transmission at the first command of the designer or something there still is backtracking point all the same looks? You can cause customary through call, to push the address of reset and to kick through jmp, and can will be perverted, to push in a stack the address on which you want to transfer control and to make ret. In what a difference? Essentially all of us equally has two stages - selection of storage and a call of the designer.

64

Re: Constant copy of a class. Who as does?

rdb_dev;
Yes, two stages, only if the compiler considers that it is not necessary to do any call', it will not do it

struct MyClass {int x; MyClass () {x = rand ();} int f () {return x;}};
int main ()
{
MyClass b;
return b.f ();
}

clang and gcc compressed it in

main:
jmp rand

Any call' the designer, any address of reset on a stack, simply stupid passage in that function with the same invariable stack.

65

Re: Constant copy of a class. Who as does?

NekZ wrote:

rdb_dev;
Yes, two stages, only if the compiler considers that it is not necessary to do any call', it will not do it
[code]
struct MyClass {int x; MyClass () {x = rand ();} int f () {return x;}};
int main ()
{
MyClass b;
return b.f ();
}
clang and gcc compressed it in
[code]
main:
jmp rand
[/code]
Any call' the designer, any address of reset on a stack, simply stupid passage in that function with the same invariable stack.

From what you took, what before a call jmp on a stack there is no address of reset from main which allows to do, in this case, ret from method MyClass:: f () directly in invocation point main returning the same int? Look a debugger!

66

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

From what you took, what before a call jmp on a stack there is no address of reset from main which allows to do, in this case, ret from method MyClass:: f () directly in invocation point main returning the same int? Look a debugger!

From that I got used to trust the eyes. And that stub, which has been added  ( _start) to the account does not go, a stupid redirect on other function and it is more than anything. Absolutely anything.

67

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
From that I got used to trust the eyes. And that stub, which has been added  ( _start) to the account does not go, a stupid redirect on other function and it is more than anything. Absolutely anything.

Certainly stupid redirect! What? If function returns the same type and grows out main, from main there will be stupid "redirect" on MyClass:: f (), as the address of reset from main already on a stack, which uses ret inside f () to return value main in invocation point. It is optimization of the given specific case.

68

Re: Constant copy of a class. Who as does?

NekZ though, most likely, the address in jmp specifies immediately in a library function rand (), instead of on MyClass:: f (). The compiler decided that in this case neither copy MyClass, nor its method f () are absolutely not necessary.

69

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

Certainly stupid redirect! What? If function returns the same type and grows out main, from main there will be stupid "redirect" on MyClass:: f (), as the address of reset from main already on a stack, which uses ret inside f () to return value main in invocation point. It is optimization of the given specific case.

Yes, the example purely academic, and presence here such here cases shows, what exactly the compiler itself solves that where and how to consider, into what phases to break such operations with objects, and in what cases all is possible , without adding a superfluous call, in what cases generally all to throw out as dead code etc. Such decisions are accepted including also on the basis of target architecture and many other factors.

70

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

NekZ though, most likely, the address in jmp specifies immediately in a library function rand (), instead of on MyClass:: f (). The compiler decided that in this case neither copy MyClass, nor its method f () are absolutely not necessary.

I to you it also try to inform.

71

Re: Constant copy of a class. Who as does?

rdb_dev wrote:

In my understanding, "emulation placement new" it when the compiler creates the call code placement new irrespective of, whether implementation of this operator by the compiler by default (on mine, it does not form till now by default and the compiler initializes a copy on a stack simply kicking the designer) is created or the developer made this implementation independently.

0) placement new it is not redefined.
1) the code placement new is a call  with specified by placement pointer on storage in a type this.
2) accordingly, the code placement new always . It actually one function invocation.
What you still should emulate?

72

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
Well how to tell. I  counted as that is the given companion object creation on a stack with a direct call  and placement new. From the point of view of result of performance of the code, probably, yes, it is equivalent, but that happens under a cowl, absolutely another, as has been illustrated above under spoilers. Before a call call moves sp for the size of the frame downwards while the call  moves sp downwards already in the function, i.e. after call and performs a heap of unnecessary operation as the size is known at a compilation stage, therefore it is so much code and  the unnecessary.
On the same logic it is possible to tell in the same way that removal of tonsils through 0 and through a mouth - things equivalent.

That that you here . It there quite adequately painted all and gave the equivalent code.
Another matter that this code  is not necessary, as it is possible to write automatic variable of this class simply.

73

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
Unless you did not understand in what an essence?
In it.
it is passed...
Once again we look in the code of the healthy person and it is not visible there operator new, unlike the underlaying.  it is not necessary to break it into separate phases  storages on a stack and a call of the designer through placement new are superfluous calls. It does as it is possible more shortly and as he considers it necessary.
P.S. It is amusing that clang with this example consults better VS'.

Once again, your purely theoretical dispute is deprived any sense, as all calls of all code of creation something on a stack and then a call  .
Absolutely on a drum as it is all happens.

74

Re: Constant copy of a class. Who as does?

MasterZiv wrote:

Once again, your purely theoretical dispute is deprived any sense, as all calls of all code of creation something on a stack and then a call  .
Absolutely on a drum as it is all happens.

I about it wrote more low, passing from the bare theory to a reality.

75

Re: Constant copy of a class. Who as does?

NekZ wrote:

it is passed...
I about it wrote more low, passing from the bare theory to a reality.

Well and?
Topic I close?