Remember that C and C++ are actually completely different languages. They share some common syntax, but C is a procedural language and C++ is object oriented, so they are different programming paradigms.

gcc should work just fine as a C compiler. I believe MinGW uses it. It also has flags you can specify to make sure it's using the right version of C (e.g. C99).

If you want to stick with C then you simply won't be able to use new (it's not part of the C language) but there shouldn't be any problems with moving to C++ for a shared library, just so long as you put your Object Oriented hat on when you do.

I'd suggest you just stick with the language you are more comfortable with. The fact that you're using new suggests that will be C++, but it's up to you.

Answer from Cameron Skinner on Stack Overflow
Top answer
1 of 7
9

Remember that C and C++ are actually completely different languages. They share some common syntax, but C is a procedural language and C++ is object oriented, so they are different programming paradigms.

gcc should work just fine as a C compiler. I believe MinGW uses it. It also has flags you can specify to make sure it's using the right version of C (e.g. C99).

If you want to stick with C then you simply won't be able to use new (it's not part of the C language) but there shouldn't be any problems with moving to C++ for a shared library, just so long as you put your Object Oriented hat on when you do.

I'd suggest you just stick with the language you are more comfortable with. The fact that you're using new suggests that will be C++, but it's up to you.

2 of 7
4
  1. You can use e.g. GCC as a C compiler. To ensure it's compiling as C, use the -x c option. You can also specify a particular version of the C standard, e.g. -std=c99. To ensure you're not using any GCC-specific extensions, you can use the -pedantic flag. I'm sure other compilers have similar options.

  2. malloc and calloc are indeed how you allocate memory in C.

  3. That's up to you.* You say that you want to be cross-platform, but C++ is essentially just as "cross-platform" as C. However, if you're working on embedded platforms (e.g. microcontrollers or DSPs), you may not find C++ compilers for them.

  4. No, new and delete are not supported in C.


* In my opinion, though, you should strongly consider switching to C++ for any application of non-trivial complexity. C++ has far more powerful high-level constructs than C (e.g. smart pointers, containers, templates) that simplify a lot of the tedious work in C. It takes a while to learn how to use them effectively, but in the long run, they will be worth it.
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 62449-new-keyword.html
'new' keyword - C Board
MyClass var1 = MyClass(constructorarg); Now what the new keyword does is dynamicly allocate memory for an object on the heap, and then return the ADDRESS to that location.
Top answer
1 of 15
101

Your observations are correct. C++ is a complicated beast, and the new keyword was used to distinguish between something that needed delete later and something that would be automatically reclaimed. In Java and C#, they dropped the delete keyword because the garbage collector would take care of it for you.

The problem then is why did they keep the new keyword? Without talking to the people who wrote the language it's kind of difficult to answer. My best guesses are listed below:

  • It was semantically correct. If you were familiar with C++, you knew that the new keyword creates an object on the heap. So, why change expected behavior?
  • It calls attention to the fact that you are instantiating an object rather than calling a method. With Microsoft code style recommendations, method names start with capital letters so there can be confusion.

Ruby is somewhere in between Python and Java/C# in it's use of new. Basically you instantiate an object like this:

f = Foo.new()

It's not a keyword, it's a static method for the class. What that means is that if you want a singleton, you can override the default implementation of new() to return the same instance every time. It's not necessarily recommended, but it's possible.

2 of 15
91

In short, you are right. The new keyword is superfluous in languages like Java and C#. Here are some insights from Bruce Eckel who was a member of C++ Standard Committee in 1990s and later published books on Java:

[T]here needed to be some way to distinguish heap objects from stack objects. To solve this problem, the new keyword was appropriated from Smalltalk. To create a stack object, you simply declare it, as in Cat x; or, with arguments, Cat x("mittens");. To create a heap object, you use new, as in new Cat; or new Cat("mittens");. Given the constraints, this is an elegant and consistent solution.

Enter Java, after deciding that everything C++ is badly done and overly complex. The irony here is that Java could and did make the decision to throw away stack allocation (pointedly ignoring the debacle of primitives, which I've addressed elsewhere). And since all objects are allocated on the heap, there's no need to distinguish between stack and heap allocation. They could easily have said Cat x = Cat() or Cat x = Cat("mittens"). Or even better, incorporated type inference to eliminate the repetition (but that -- and other features like closures -- would have taken "too long" so we are stuck with the mediocre version of Java instead; type inference has been discussed but I will lay odds it won't happen. And shouldn't, given the problems in adding new features to Java).

🌐
Unstop
unstop.com › home › blog › new operator in c++ explained (+code examples)
New Operator In C++ Explained (+Code Examples) // Unstop
June 6, 2025 - This keyword ensures that if the memory allocation fails, it does not throw an exception but instead returns a null pointer. Then, we use an if-else statement to check if the allocation was successful by using the inequality relational operator to see if obj is not null. If the condition is true, it means allocation successful, and the if-block is executed where the new operator returns a valid pointer to the object.
🌐
cppreference.com
en.cppreference.com › w › c › keyword.html
C keywords - cppreference.com
This is a list of reserved keywords in C. Since they are used by the language, these keywords are not available for re-definition.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › new-vs-operator-new-in-cpp
new vs operator new in C++ - GeeksforGeeks
February 21, 2023 - The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer ...
🌐
Quora
quora.com › What-are-some-alternatives-to-using-the-new-keyword-in-C
What are some alternatives to using the 'new' keyword in C++? - Quora
Answer: In C++ you can call a function that creates objects and returns them. The compiler is an optimizing compiler, and will arrange the code to not need to return a copy in most cases. [code]#include #include #include #include using st_vec = st...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › keywords › new-modifier
new modifier - C# reference | Microsoft Learn
... When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.
Find elsewhere
🌐
Cppreference
en.cppreference.com › w › cpp › keyword › new.html
C++ keyword: new - cppreference.com
C++ language · [edit] Keywords · [edit] new expression · allocation functions as the name of operator-like functions · Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/keyword/new&oldid=175035" Support us · Recent changes · FAQ · Offline version ·
🌐
Bitesizedengineering
bitesizedengineering.com › p › what-happens-when-you-type-the-new
What happens when you type the "new" keyword?
February 9, 2023 - The first one is, you guessed it, operator new, and it’s job is to find a place to house your bits, and the second one is placement new, which actually CONSTRUCTS the object in the memory space returned by operator new.
🌐
Javiercasas
javiercasas.com › articles › the-tyranny-of-the-new-keyword-1
The Tyranny of the new keyword (I)
Every modern programming language has a way to construct new values, because every minimally complex program is designed to process basic values into more complex results. The usual operation to summon creating a new value is the new keyword, at least in Java-like languages, such as Java, C++, C#, TypeScript and others.
🌐
Cppreference
en.cppreference.com › w › cpp › language › new.html
new expression - cppreference.com
The first dimension of zero is acceptable, and the allocation function is called. The new expression allocates storage by calling the appropriate allocation function. If type is a non-array type, the name of the function is operator new.
🌐
IBM
ibm.com › docs › en › wdfrhcw › 1.4.0
C reserved keywords
We cannot provide a description for this page right now
🌐
PREP INSTA
prepinsta.com › home › c++ online tutorials › new keyword in c++
New keyword in C++ programming language | Prepinsta
February 16, 2023 - New: The new keyword in C++ allocates the required bytes of memory by returning a pointer to the allocated memory
🌐
Quora
quora.com › What-exactly-does-the-keyword-new-do-in-C++-During-an-interview-I-said-it-instantiates-a-class-and-calls-the-constructor-Why-is-that-wrong
What exactly does the keyword new do in C++? During an interview, I said it instantiates a class and calls the constructor. Why is that wrong? - Quora
Answer (1 of 13): In summary [code ]new[/code] allocates memory for the specified object from the free-store and then initializes the object (if primitive) or calls the relevant constructor (if a class object). In C++ classes aren’t themselves objects (like Java say) and while any static members...
🌐
Scaler
scaler.com › topics › cpp › new-operator-in-cpp
new Operator in C++ - Scaler Topics
June 27, 2022 - Explanation: In this example, the ... with new. This is clear because the memory address of both &var and mem is equal. type-id: Type-id specifies the data type to be allocated, it can either be a built-in data type or can be a user-defined data type, and if the type specification is complex, it can be enclosed in parenthesis to force the order of binding. Auto keyword can be used ...
🌐
Cplusplus
cplusplus.com › forum › beginner › 24413
"new" keyword - C++ Forum
In Java, all objects are created on the heap and when you make a declaration like cell c1, it's implied it's a pointer. In C++, that use is optional and you have to explicitly declare it as a pointer, cell* c1. Your example should be cell* c1 = new cell;
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › language-reference › operators › new-operator
new operator - Create and initialize a new instance of a type - C# reference | Microsoft Learn
List<int> xs = new(); List<int> ys = new(capacity: 10_000); List<int> zs = new() { Capacity = 20_000 }; Dictionary<int, List<int>> lookup = new() { [1] = new() { 1, 2, 3 }, [2] = new() { 5, 8, 3 }, [5] = new() { 1, 0, 4 } }; As the preceding example shows, you always use parentheses in a target-typed new expression. If a target type of a new expression is unknown (for example, when you use the var keyword), you must specify a type name.
🌐
Wikibooks
en.wikibooks.org › wiki › C_Sharp_Programming › Keywords › new
C# Programming/Keywords/new - Wikibooks, open books for an open world
There are template/file changes awaiting review. C# Programming Cover | Introduction | Basics | Classes | Advanced Topics | The .NET Framework | Index · The new keyword has two different meanings: It is an operator that requests a new instance of the class identified by its argument.