Die kurze Antwort lautet: Die meisten von C unterstützten Sprachkonstrukte werden auch vom Mikroprozessor des Zielcomputers unterstützt. Daher wird kompilierter C-Code sehr gut und effizient in die Assemblersprache des Mikroprozessors übersetzt, was zu kleinerem Code und geringerem Platzbedarf führt.
Die längere Antwort erfordert ein wenig Assembler-Kenntnisse. In C eine Aussage wie diese:
int myInt = 10;
würde in der Montage so etwas übersetzen:
myInt dw 1
mov myInt,10
Vergleichen Sie dies mit etwas wie C ++:
MyClass myClass;
myClass.set_myInt(10);
Der resultierende Assembler-Code (abhängig von der Größe von MyClass ()) kann zu Hunderten von Assembler-Zeilen führen.
Ohne Programme in Assemblersprache zu erstellen, ist reines C wahrscheinlich der "dünnste" und "engste" Code, in dem Sie ein Programm erstellen können.
BEARBEITEN
Angesichts der Kommentare zu meiner Antwort beschloss ich, einen Test durchzuführen, nur aus Gründen meiner eigenen Gesundheit. Ich habe ein Programm namens "test.c" erstellt, das so aussah:
#include <stdio.h>
void main()
{
int myInt=10;
printf("%d\n", myInt);
}
Ich habe dies bis zur Assemblierung mit gcc kompiliert. Ich habe die folgende Befehlszeile verwendet, um es zu kompilieren:
gcc -S -O2 test.c
Hier ist die resultierende Assemblersprache:
.file "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "%d\n"
.section .text.unlikely,"ax",@progbits
.LCOLDB1:
.section .text.startup,"ax",@progbits
.LHOTB1:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB24:
.cfi_startproc
movl $10, %edx
movl $.LC0, %esi
movl $1, %edi
xorl %eax, %eax
jmp __printf_chk
.cfi_endproc
.LFE24:
.size main, .-main
.section .text.unlikely
.LCOLDE1:
.section .text.startup
.LHOTE1:
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits
Ich erstelle dann eine Datei namens "test.cpp", die eine Klasse definiert und dasselbe wie "test.c" ausgibt:
#include <iostream>
using namespace std;
class MyClass {
int myVar;
public:
void set_myVar(int);
int get_myVar(void);
};
void MyClass::set_myVar(int val)
{
myVar = val;
}
int MyClass::get_myVar(void)
{
return myVar;
}
int main()
{
MyClass myClass;
myClass.set_myVar(10);
cout << myClass.get_myVar() << endl;
return 0;
}
Ich habe es auf die gleiche Weise mit diesem Befehl kompiliert:
g++ -O2 -S test.cpp
Hier ist die resultierende Assembly-Datei:
.file "test.cpp"
.section .text.unlikely,"ax",@progbits
.align 2
.LCOLDB0:
.text
.LHOTB0:
.align 2
.p2align 4,,15
.globl _ZN7MyClass9set_myVarEi
.type _ZN7MyClass9set_myVarEi, @function
_ZN7MyClass9set_myVarEi:
.LFB1047:
.cfi_startproc
movl %esi, (%rdi)
ret
.cfi_endproc
.LFE1047:
.size _ZN7MyClass9set_myVarEi, .-_ZN7MyClass9set_myVarEi
.section .text.unlikely
.LCOLDE0:
.text
.LHOTE0:
.section .text.unlikely
.align 2
.LCOLDB1:
.text
.LHOTB1:
.align 2
.p2align 4,,15
.globl _ZN7MyClass9get_myVarEv
.type _ZN7MyClass9get_myVarEv, @function
_ZN7MyClass9get_myVarEv:
.LFB1048:
.cfi_startproc
movl (%rdi), %eax
ret
.cfi_endproc
.LFE1048:
.size _ZN7MyClass9get_myVarEv, .-_ZN7MyClass9get_myVarEv
.section .text.unlikely
.LCOLDE1:
.text
.LHOTE1:
.section .text.unlikely
.LCOLDB2:
.section .text.startup,"ax",@progbits
.LHOTB2:
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB1049:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $10, %esi
movl $_ZSt4cout, %edi
call _ZNSolsEi
movq %rax, %rdi
call _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1049:
.size main, .-main
.section .text.unlikely
.LCOLDE2:
.section .text.startup
.LHOTE2:
.section .text.unlikely
.LCOLDB3:
.section .text.startup
.LHOTB3:
.p2align 4,,15
.type _GLOBAL__sub_I__ZN7MyClass9set_myVarEi, @function
_GLOBAL__sub_I__ZN7MyClass9set_myVarEi:
.LFB1056:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $_ZStL8__ioinit, %edi
call _ZNSt8ios_base4InitC1Ev
movl $__dso_handle, %edx
movl $_ZStL8__ioinit, %esi
movl $_ZNSt8ios_base4InitD1Ev, %edi
addq $8, %rsp
.cfi_def_cfa_offset 8
jmp __cxa_atexit
.cfi_endproc
.LFE1056:
.size _GLOBAL__sub_I__ZN7MyClass9set_myVarEi, .-_GLOBAL__sub_I__ZN7MyClass9set_myVarEi
.section .text.unlikely
.LCOLDE3:
.section .text.startup
.LHOTE3:
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I__ZN7MyClass9set_myVarEi
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1"
.section .note.GNU-stack,"",@progbits
Wie Sie deutlich sehen können, ist die resultierende Assembly-Datei in der C ++ - Datei viel größer als in der C-Datei. Selbst wenn Sie alle anderen Dinge ausschneiden und nur das C "main" mit dem C ++ "main" vergleichen, gibt es viele zusätzliche Dinge.