Click here to Skip to main content
15,902,635 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: ANSI compliant XML Parser? Pin
Anonymous10-Mar-03 3:52
Anonymous10-Mar-03 3:52 
GeneralRe: ANSI compliant XML Parser? Pin
Paul Selormey10-Mar-03 4:11
Paul Selormey10-Mar-03 4:11 
GeneralSave increment filename ... Pin
macmac386-Mar-03 3:39
macmac386-Mar-03 3:39 
GeneralRe: Save increment filename ... Pin
Paul Selormey10-Mar-03 0:47
Paul Selormey10-Mar-03 0:47 
GeneralAspireing c++ programmer (doesn't know Jack atm) Question Pin
Caden1-Mar-03 9:56
Caden1-Mar-03 9:56 
GeneralRe: Aspireing c++ programmer (doesn't know Jack atm) Question Pin
Paul Selormey2-Mar-03 23:47
Paul Selormey2-Mar-03 23:47 
GeneralRe: Aspireing c++ programmer (doesn't know Jack atm) Question Pin
Caden3-Mar-03 8:06
Caden3-Mar-03 8:06 
GeneralCreating objects on stack Pin
VizOne28-Feb-03 6:05
VizOne28-Feb-03 6:05 
Hi!

I am working on a function that returns an object initilized with some
specific values, like
<br />
__value struct MyStruct<br />
{<br />
public:<br />
   MyStruct(int a, int b, int c)<br />
   : a(in_a), b(in_b), c(in_c) {}<br />
<br />
  static MyStruct One() { return MyStruct(1,1,1); }<br />
<br />
private:<br />
  int a, b, c;<br />
};<br />

This is compiled to the following MSIL-code:

<br />
.method public static valuetype Cpp.MyStruct<br />
        One() cil managed<br />
{<br />
  // Code size       26 (0x1a)<br />
  .maxstack  4<br />
  .locals (valuetype Cpp.MyStruct V_0)<br />
  IL_0000:  ldloca.s   V_0<br />
  IL_0002:  initobj    Cpp.MyStruct<br />
  IL_0008:  ldloca.s   V_0<br />
  IL_000a:  ldc.i4.1<br />
  IL_000b:  ldc.i4.1<br />
  IL_000c:  ldc.i4.1<br />
  IL_000d:  call       instance void Cpp.MyStruct::.ctor(int32,<br />
                                                         int32,<br />
                                                         int32)<br />
  IL_0012:  ldloca.s   V_0<br />
  IL_0014:  ldobj      Cpp.MyStruct<br />
  IL_0019:  ret<br />
} // end of method MyStruct::One<br />

I looked at IL_0002 and found a initobj. I tried to avoid initializing of
the members, so I defined an empty default c'tor:
MyStruct() {}

However, now my IL code expanded to:
<br />
.method public static valuetype Cpp.MyStruct<br />
        One() cil managed<br />
{<br />
  // Code size       35 (0x23)<br />
  .maxstack  4<br />
  .locals (valuetype Cpp.MyStruct V_0,<br />
           valuetype Cpp.MyStruct V_1)<br />
  IL_0000:  ldloca.s   V_1<br />
  IL_0002:  initobj    Cpp.MyStruct<br />
  IL_0008:  ldloca.s   V_1<br />
  IL_000a:  ldc.i4.1<br />
  IL_000b:  ldc.i4.1<br />
  IL_000c:  ldc.i4.1<br />
  IL_000d:  call       instance void Cpp.MyStruct::.ctor(int32,<br />
                                                         int32,<br />
                                                         int32)<br />
  IL_0012:  ldloca.s   V_0<br />
  IL_0014:  ldloca.s   V_1<br />
  IL_0016:  cpobj      Cpp.MyStruct<br />
  IL_001b:  ldloca.s   V_0<br />
  IL_001d:  ldobj      Cpp.MyStruct<br />
  IL_0022:  ret<br />
} // end of method MyStruct::One<br />

Which contains a cpobj and two local instances of MyStruct and therefore
seems to be even worse.

I wrote a similar struct in C# to compare compilation output:
<br />
public  struct MyStruct<br />
{<br />
  public MyStruct(int in_a, int in_b, int in_c)<br />
  {<br />
    a = in_a;<br />
    b = in_b;<br />
    c = in_c;<br />
  }<br />
  public static MyStruct One() { return new MyStruct(1, 1, 1); }<br />
  int a, b, c;<br />
};<br />

This compiled to the following code:
<br />
.method public hidebysig static valuetype CSharp.MyStruct<br />
        One() cil managed<br />
{<br />
  // Code size       9 (0x9)<br />
  .maxstack  8<br />
  IL_0000:  ldc.i4.1<br />
  IL_0001:  ldc.i4.1<br />
  IL_0002:  ldc.i4.1<br />
  IL_0003:  newobj     instance void CSharp.MyStruct::.ctor(int32,<br />
                                                            int32,<br />
                                                            int32)<br />
  IL_0008:  ret<br />
} // end of method MyStruct::One<br />

Now I tested execution time and I am not really suprised: the the C++ struct
without default c'tor was about 30% slower than the C# version. The C++
Version with default c'tor even was 70% slower than the C# version.

Now I have two questions: first, is the C# version creating the object on
the heap or the stack?
And: how can I make the C++ version compile to the faster version?
Thanks in advance!

- Andre
GeneralRe: Creating objects on stack Pin
Jeff J1-Mar-03 16:26
Jeff J1-Mar-03 16:26 
GeneralRe: Creating objects on stack Pin
Paul Selormey2-Mar-03 23:40
Paul Selormey2-Mar-03 23:40 
GeneralRe: Creating objects on stack Pin
Jeff J3-Mar-03 11:49
Jeff J3-Mar-03 11:49 
GeneralRe: Creating objects on stack Pin
Jeff J3-Mar-03 11:43
Jeff J3-Mar-03 11:43 
GeneralRe: Creating objects on stack Pin
Paul Selormey3-Mar-03 23:47
Paul Selormey3-Mar-03 23:47 
GeneralRe: Creating objects on stack Pin
VizOne10-Mar-03 1:45
VizOne10-Mar-03 1:45 
GeneralRe: Creating objects on stack Pin
Paul Selormey9-Mar-03 17:07
Paul Selormey9-Mar-03 17:07 
GeneralMixins and __gc Pin
-=jarl=-27-Feb-03 3:59
-=jarl=-27-Feb-03 3:59 
GeneralRe: Mixins and __gc Pin
Paul Selormey27-Feb-03 22:11
Paul Selormey27-Feb-03 22:11 
GeneralRe: Mixins and __gc Pin
-=jarl=-6-Mar-03 1:14
-=jarl=-6-Mar-03 1:14 
GeneralHandles and pointers... Pin
-=jarl=-26-Feb-03 7:28
-=jarl=-26-Feb-03 7:28 
GeneralRe: Handles and pointers... Pin
Jeff J26-Feb-03 16:46
Jeff J26-Feb-03 16:46 
GeneralRe: Handles and pointers... Pin
-=jarl=-26-Feb-03 23:19
-=jarl=-26-Feb-03 23:19 
GeneralManaged and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:55
-=jarl=-26-Feb-03 6:55 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:58
-=jarl=-26-Feb-03 6:58 
GeneralRe: Managed and Unmanaged structures Pin
Paul Selormey26-Feb-03 13:51
Paul Selormey26-Feb-03 13:51 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 23:16
-=jarl=-26-Feb-03 23:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.