C++が重いとか、余分なRAMを使うと言われる理由を探してみます。
簡単なコードとアセンブリを用意。
MinGWでコンパイルしたので余分なアセンブリが入っていると思います。
class test
{
public:
int data1;
int data2;
void aaa()
{
data1 = 1234;
}
;
};
volatile void sub3()
{
test* ts = new test;
ts->aaa();
ts->data1 = 300;
}
volatile void sub2()
{
test ts;
ts.aaa();
ts.data1 = 200;
}
volatile void sub1()
{
test ts;
ts.aaa();
ts.data1 = 100;
}
int main()
{
sub1();
sub2();
sub3();
return 0;
}
.file "test02.cpp" .text .align 2 .globl __Z4sub3v .def __Z4sub3v; .scl 2; .type 32; .endef __Z4sub3v: pushl %ebp movl %esp, %ebp subl $8, %esp movl $8, (%esp) call __Znwj movl %eax, -4(%ebp) movl -4(%ebp), %eax movl %eax, (%esp) call __ZN4test3aaaEv movl -4(%ebp), %eax movl $300, (%eax) leave ret .section .text$_ZN4test3aaaEv,"x" .linkonce discard .align 2 .globl __ZN4test3aaaEv .def __ZN4test3aaaEv; .scl 2; .type 32; .endef __ZN4test3aaaEv: pushl %ebp movl %esp, %ebp movl 8(%ebp), %eax movl $1234, (%eax) popl %ebp ret .text .align 2 .globl __Z4sub2v .def __Z4sub2v; .scl 2; .type 32; .endef __Z4sub2v: pushl %ebp movl %esp, %ebp subl $24, %esp leal -8(%ebp), %eax movl %eax, (%esp) call __ZN4test3aaaEv movl $200, -8(%ebp) leave ret .align 2 .globl __Z4sub1v .def __Z4sub1v; .scl 2; .type 32; .endef __Z4sub1v: pushl %ebp movl %esp, %ebp subl $24, %esp leal -8(%ebp), %eax movl %eax, (%esp) call __ZN4test3aaaEv movl $100, -8(%ebp) leave ret .def ___main; .scl 2; .type 32; .endef .align 2 .globl _main .def _main; .scl 2; .type 32; .endef _main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax addl $15, %eax addl $15, %eax shrl $4, %eax sall $4, %eax movl %eax, -4(%ebp) movl -4(%ebp), %eax call __alloca call ___main call __Z4sub1v call __Z4sub2v call __Z4sub3v movl $0, %eax leave ret .def __Znwj; .scl 2; .type 32; .endef
見たところ、むしろ非常に効率よく処理してるような気がします。
少なくとも、ROMからRAMへコードはコピーされないことが確認できます。
テンプレートや例外はまた今度


