LeviX
LeviX

Reputation: 3167

Updating legacy C code headers to C++ stdlib

Inherited a slew of legacy C code, and am in the process of porting it to compile on Linux with GCC (g++). Since we are going to be using C++ in the future and I'm fixing the "custom" library header files anyways (and compiler warnings), is it safe to update the old C headers to use the newer C++ style ones.

So things like

#include <cstdlib>

Instead of

#include <stdlib.h>

To my knowledge the only difference between the two is that cstdlib has things in the std:: namespace.

Would anything make this a bad idea?

Upvotes: 2

Views: 339

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283614

Your code may change in very subtle ways, due to the fact that the C++ standard headers use overloading where C used different names. This is most likely to cause trouble with cmath.

stdlib.h isn't going anywhere, so feel free to keep using it.

For example, compare:

#include <iostream>
using namespace std;

#include <stdlib.h>
#include <math.h>

int main(void)
{
    double x = -2;
    cout << (3/abs(x)) << endl;
    return 0;
}

The results before and after switching to C++ headers are very different, even though the exact same C++ compiler and options are used in both cases.

Upvotes: 1

Dhaivat Pandya
Dhaivat Pandya

Reputation: 6536

They're exactly the same (on most systems) except for the namespace thing.

Upvotes: 1

Related Questions