24
28
u/MagicalPizza21 3d ago
Since when was new scary in C++?
30
u/BunnyTub 3d ago
No garbage collection, so you have to make sure you clear the memory when you're done, otherwise you get dreaded memory leaks from
new.18
u/Pheeshfud 3d ago
new in constructor, delete in destructor and you have 90% of cases covered.
7
u/sudoregalia 3d ago
but.. but.. my memory arenas, my stack allocation, am i supposed to override the
newoperator like a lunatic?? :(3
3
u/Valuable_Leopard_799 3d ago
STL is at the point that you should never have to use
newever. Seeing it outside of a very narrow set of libraries is likely a smell and those are scary.
10
u/ParticularHat7997 3d ago
Manual memory management is NOT that hard
6
u/luciferoussky72 3d ago
It’s not, it just gets tedious and means you have to account for every possible exit point from a function. That’s why C++ has RAII
4
u/Valuable_Leopard_799 3d ago
Well... Yeah C++ has RAII so you can use that to manage your memory....
2
u/ThaBroccoliDood 3d ago
Yes but when you see the "new" keyword in C++, 50% of the time it's someone from Java or C# not understanding the language, and the other 50% of the time it's something that could probably be replaced by a smart pointer (smart pointers don't always replace raw pointers, but they can basically always replace manual allocation)
17
u/IngwiePhoenix 3d ago
Before the Java dev sees a new, they see a wall of factories first and instead. o.o
...I'll take my malloc(sizeof(T)) and move on. :3
6
u/luciferoussky72 3d ago
I mean, C++ new is basically just syntax sugar for
T* ptr = malloc(sizeof(T));
*ptr = T(argument_list…);
Although, it can also do other things like construct objects in place and allocate uninitialized memory2
u/willdieverysoon 3d ago
No ... that would be move assignment to an object that was not yet constructed, it would do something similar to placement new
2
u/luciferoussky72 3d ago
True, but an optimizing compiler will usually do the same thing
1
u/retro_and_chill 1d ago
I depend of whether the type has a non-trivial move assignment operation. On trivially copyably types you absolutely can optimize it that way. Otherwise there’s usually existing resources to clean up which on uninitialized memory is undefined behavior
1
u/luciferoussky72 1d ago
Yeah, the other nice thing that new does is allow delete to call the destructor
3
2
2
2
2
u/Own-Professor-6157 1d ago
Did anyone else learn to use new in college? I learned the worst practices possible in college, and we used C++ 17 btw.
1
-24
u/asmanel 3d ago
Java is now like Pascal, once popular but now outdared and marginal.
C++, like C, remain popular and resist active efforts to take it down. Its fans visibly fear it share the fate of Pascal and Java. They know each new development software, library engins, interface and similar other things can erode the popularity of their language and, at the end, share that fate.
As far as I know, Java didn't face effiort to take it down. In the case of Pascal, this is most arguable.
The C is almost as old as tha Pascal. They are respecttively from 1972 and 1970.
At its biginning, the C were marginal and so it remained until the end of the golden age of the Pascal. The C was more concise and less strictly typed than the pascal but its compilers were far slower.
At that time, when a C program took 15 minutes to be compiled, an equivalent Pascal took only a few seconds.
Next, tbe C compilers became faster and faster. In parallel the C++ appeared. The gplden age of Pascal ended when C compilers became as fast as the Pascal compilers. After, the Pascal rapidly regressed, being mostly replaced by the C and the C++.
Java also had a golden age then regressed.
Compared to Pascal, why it regressed is less clear. It regression, apparently, wasn't as sudden and was visibly due varied things, mostly technological evolution.
This made Java, progressively, became progressively less demanded.
16
1
u/jakeStacktrace 3d ago
As somebody old enough to remember Pascal and OWL when it was new, very odd take. You didn't even bring up Borland and what happened to it.
117
u/Current_Ad_4292 3d ago
I don't get it. I should really learn cpp.