Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why object file size changes for the same code in C and C++

Tags:

c++

c

I have written same lines of code in both C and C++ build environment of visual studio 2008 but C code's object file size is 5.5kb and C++ code file size is 6.17 kb. Why is this difference?

Following is the code in both environments:

#include <stdio.h>

struct S
{
    char c;
};
void func()
{
    int i;
    int j;
    printf("%d", i);
}

int main()
{
    struct S s;
    return 0;
}
like image 756
Vikas Avatar asked Dec 10 '10 05:12

Vikas


2 Answers

It links different C runtime libraries in each case. Check here for detailed explanation. For instance, libcmt.lib vs libcpmt.lib.

like image 104
Kirill V. Lyadvinsky Avatar answered Sep 27 '22 17:09

Kirill V. Lyadvinsky


Just because your code has different meaning in C and C++.

  • In C++ you declare functions that receive no arguments, in C you declare functions that have an unspecified number of arguments. (Never change the signature of main)
  • function names are mangled in C++
  • functions may throw exceptions in C++
  • executables are linked against different libraries by default
  • because of the lack of initialization of i the call to printf has undefined behavior. Both languages might decide on different strategies to shoot you.
like image 32
Jens Gustedt Avatar answered Sep 27 '22 16:09

Jens Gustedt