开发者

Is there any (performance-related) downside to compiling C code with the Visual Studio C++ compiler?

开发者 https://www.devze.com 2023-02-23 14:13 出处:网络
This is my first time developing with Visual Studio (2010) and I noticed that: int *x = malloc(sizeof(int) * 50);

This is my first time developing with Visual Studio (2010) and I noticed that:

int *x = malloc(sizeof(int) * 50);

gives an error when I try to build "main.c". I'm assuming this is because VS is using its C++ compiler to build C programs.

Is the code generated by VS for C as good as it gets under Windows or should I look for something bett开发者_运维问答er and/or exclusive to C?


I'm assuming this is because VS is using its C++ compiler to build C programs.

Your assumption is incorrect: there is nothing wrong with that line of code and in fact Visual C++ will compile it without error:

c:\dev>type stubby.c
#include <stdlib.h>

int main(void)
{
    int *x = malloc(sizeof(int) * 50);
}

c:\dev>cl stubby.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

stubby.c
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:stubby.exe
stubby.obj

c:\dev>

Whether Visual C++ is suitable for your needs depends on what your needs are. It does not support C99; if support for C99 is important to you, then you'll want to find another compiler. It does not support the plethora of GNU language extensions; if those are important to you, you'll want to find another compiler.


  1. This has nothing to do with performance; it's a language restriction. In C++, you need a cast:

    int *x = (int*)malloc(sizeof(int) * 50);
    
  2. There are other compilers for Windows than the Visual C++ compiler; take a look at G++ (part of MinGW GCC), for example.

  3. In general, C compilers can optimize better than C++ compilers can. But I don't think using the same exact compiler in C++ mode makes any difference than running it in C mode, at least not typically.


I don't use the Visual Studio compiler purely because it's not C99 compliant, and I find C99 much easier to work with.

Personally I use MinGW + GCC instead, but I use Notepad++ instead of Visual Studio. I know that its definitely possible to get Visual Studio to compile using GCC, but I'm not aware of anyone who has done this yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消