0

Many a times, I have seen in C++ code pointers allocations for structs as;

struct Current{
int amp;
};

Current *cur = new Current();

However, "Current" is a structure and not class here, but is supported in C++. How good is this to be used in place of C-type allocation, as below:

Current *cur = (Current*) malloc (sizeof(Current));

What is the underlying concept behind this? One known to me is, that the "structs" are considered as "classes" in C++.

Concept of struct-VS-class in C++ is known. Query is majorly about the specific use case, as both are permissible, but which should be preferred over, and why?

parasrish
  • 3,864
  • 26
  • 32
  • 1
    possible duplicate of [C/C++ Struct vs Class](http://stackoverflow.com/questions/2750270/c-c-struct-vs-class) – timday Apr 05 '15 at 16:59
  • 1
    possible duplicate of [What are the differences between struct and class in C++?](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c) – timrau Apr 05 '15 at 16:59
  • In C++, using new/delete is recommended over using malloc()/free(), even for structs (which are the same as classes except that member variables are public by default) – Jeremy Friesner Apr 05 '15 at 17:00
  • struct/class, same thing really. Anyway, why do you use dynamic allocation in the first place? Are you sure you need it? Usually people who are new to C++ like to use the `new` keyword for no reason. That is why it is called `new`. It is short for `newbie`. – juanchopanza Apr 05 '15 at 17:05

2 Answers2

5

In C++, a struct is the same as a class, except that it has default public members, as well as default public inheritance.

As for the use of malloc, although it is possible to use it, it is as you say a C-type allocation, and shouldn't be used for C++. If you must, you would use malloc/free the same way for a struct as for a class.

However, the correct C++ way is to use new and delete, which are also used the same for a struct as for a class.


If you're curious as to why new/delete should be used instead of malloc/free, I refer you to this Q/A: In what cases do I use malloc vs new?


As far as the use of parenthesis, i.e.:

Current *cur = new Current();
//vs.
Current *cur = new Current;

See this: Do the parentheses after the type name make a difference with new?

I just did myself, and learned something new.

Community
  • 1
  • 1
swalog
  • 4,403
  • 3
  • 32
  • 60
  • New also invokes constructor in contrary to malloc – SomeWittyUsername Apr 05 '15 at 17:06
  • 3
    The parentheses actually make a difference in this case. – juanchopanza Apr 05 '15 at 17:07
  • 1
    Could you expand a bit on why there is nothing wrong the parens @LightningRacisinObrit ? It seems you could be right. But I'm unsure as to why this is. – swalog Apr 05 '15 at 17:10
  • `new Current()` will initialize `amp` to 0, while `new Current` will not. The parentheses are actually better. – rici Apr 05 '15 at 17:10
  • @swalog It is wrong to say they should not be used. In this case, using them results in zero-initialization of `amp`. Without them, `amp` is left uninitialized. – juanchopanza Apr 05 '15 at 17:11
  • thanks @swalog. I have now clear understanding, to the 4 related information : new with and without "()" operator, when and why to use delete/free and new/malloc and what is POD. Thanks all. – parasrish Apr 06 '15 at 16:13
3

You've misunderstood structs.

However, "Current" is a structure and not class here, but is supported in C++

Wrong. Current is a class. It would also be a class if you'd declared it with the keyword class instead of the keyword struct.

The keyword struct is provided for backward compatibility, and using it changes some visibility defaults in the definition syntax. But, otherwise, you are still just defining a class.

Certainly there is nothing magical about classes defined with struct that means you should go back to archaic C-style allocations.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055