plain c using new keyword
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.
When should I use the new keyword in C++?
Method 1 (using new
)
- Allocates memory for the object on the free store (This is frequently the same thing as the heap)
- Requires you to explicitly
delete
your object later. (If you don't delete it, you could create a memory leak) - Memory stays allocated until you
delete
it. (i.e. you couldreturn
an object that you created usingnew
) - The example in the question will leak memory unless the pointer is
delete
d; and it should always be deleted, regardless of which control path is taken, or if exceptions are thrown.
Method 2 (not using new
)
- Allocates memory for the object on the stack (where all local variables go) There is generally less memory available for the stack; if you allocate too many objects, you risk stack overflow.
- You won't need to
delete
it later. - Memory is no longer allocated when it goes out of scope. (i.e. you shouldn't
return
a pointer to an object on the stack)
As far as which one to use; you choose the method that works best for you, given the above constraints.
Some easy cases:
- If you don't want to worry about calling
delete
, (and the potential to cause memory leaks) you shouldn't usenew
. - If you'd like to return a pointer to your object from a function, you must use
new
The new keyword means something in ANSI C?
new
is not part of C
keywords.
Your IDE probably tries to parse this file as C++.
Anyway, it's good idea to avoid using C++ keywords even in C code for better interoperability.
When to use new and when not to, in C++? [duplicate]
You should use new
when you wish an object to remain in existence until you delete
it. If you do not use new
then the object will be destroyed when it goes out of scope. Some examples of this are:
void foo()
{
Point p = Point(0,0);
} // p is now destroyed.
for (...)
{
Point p = Point(0,0);
} // p is destroyed after each loop
Some people will say that the use of new
decides whether your object is on the heap or the stack, but that is only true of variables declared within functions.
In the example below the location of 'p' will be where its containing object, Foo, is allocated. I prefer to call this 'in-place' allocation.
class Foo
{
Point p;
}; // p will be automatically destroyed when foo is.
Allocating (and freeing) objects with the use of new
is far more expensive than if they are allocated in-place so its use should be restricted to where necessary.
A second example of when to allocate via new
is for arrays. You cannot* change the size of an in-place or stack array at run-time so where you need an array of undetermined size it must be allocated via new.
E.g.
void foo(int size)
{
Point* pointArray = new Point[size];
...
delete [] pointArray;
}
(*pre-emptive nitpicking - yes, there are extensions that allow variable sized stack allocations).
C++ pointer function and new keyword
Yes, if you create an object with new
keyword, you will have to delete it with delete
keyword. There is no garbage collection in C++. So in your case, this would be:
delete car;
Also, the difference between creating with new
and just using the constructor directly as you suggest is that with new, the object is created on the heap, and it's lifetime is extended until it is explicitly deleted by the programmer. In the other case, it will be created on the stack and will be deleted automatically as soon as the enclosing funcion or block is exited.
What happens in your case is that you create an object on the heap and never delete it. This causes a so-called memory leak. Since your program is small, this is not an issue and this memory is released after the program finishes. However, in a case of a long running program, or a program that often allocates on the heap, it can cause the program to run out of available memory.
Also note that you could create an object inside the function, change the signature to return an object instead of a pointer, and have the function return that object directly. This would work, but what would happen is that first an object local to the function would be created on the stack. Then that object would be copied into another object created in the main function, and the first object would then be deleted. This is not very efficient, that is why a pointer to an objecet allocated on the heap is used. One reason more to use the heap would be for storing large objects. The stack is small compared to the heap, and should not be used to store very large objects.
I hope this clarifies it a bit, but to understand all this well takes a lot of time and work, on stackoverflow answer is not enough. Iwould suggest to read more about differences between objects on heap and object on stack in C++. There is an abundance of information online.
Related Topics
How to Add Reflection to a C++ Application
How to Determine Cpu and Memory Consumption from Inside a Process
Uint8_T Can't Be Printed With Cout
Why Does Reading a Record Struct Fields from Std::Istream Fail, and How to Fix It
What's the Difference Between "Stl" and "C++ Standard Library"
Easiest Way to Convert Int to String in C++
What Does Int Argc, Char *Argv[] Mean
Why Do We Need Virtual Functions in C++
What Are the Differences Between Struct and Class in C++
Capture Characters from Standard Input Without Waiting For Enter to Be Pressed
Why Can't Variables Be Declared in a Switch Statement
How Do Malloc() and Free() Work
#Pragma Once VS Include Guards
Get Current Time in Milliseconds, or Hh:Mm:Ss:Mmm Format
How to Read in User Entered Comma Separated Integers