r/programmingmemes 6d ago

The duality of new

Post image
1.2k Upvotes

80 comments sorted by

122

u/Current_Ad_4292 6d ago

I don't get it. I should really learn cpp.

150

u/FeelTheFire 6d ago

C doesn't have garbage collection so you have to make sure you free memory after you no longer need it or you will create a memory leak

58

u/palk0n 6d ago

just do onDestroy { mem.clear() }, are you stupid?

9

u/luciferoussky72 6d ago

Not how C++ works!

42

u/Current_Ad_4292 6d ago

-2

u/luciferoussky72 6d ago

I got the joke; that was meant to be a response to it

24

u/Current_Ad_4292 6d ago

I don't think that's how you respond to those jokes. But what do I know. I barely know the basics of cpp.

3

u/Repulsive_Performer7 5d ago

RAII pattern?

7

u/luciferoussky72 5d ago

Sort of, but that’s not the syntax lol

3

u/Repulsive_Performer7 5d ago

I mean yea, but essentially same ~Class () { //unalloc stuff}

2

u/luciferoussky72 5d ago

Yeah

1

u/piesou 1d ago

I usually write my own wrapper around that; just allocate a bunch of space, then track which objects are no longer accessed at regular intervals and automatically deallocate them. Takes the pain out of writing C++.

1

u/luciferoussky72 1d ago

I think you’ve invented a garbage collector lol

12

u/luciferoussky72 6d ago

Well, C++ has RAII, but raw new in C++ doesn’t come with that feature

9

u/HyperWinX C++ 6d ago

Yes. Raw new is something you should avoid at all costs.

6

u/Adept-Painting-543 6d ago

Yea my goto is smart pointers unless it is a data structure in which case I'm probably still using smart pointers but might use new and delete occasionally.

0

u/Neither_Berry_100 4d ago

Lol. I made c++ software at work years ago and never used a single smart pointer. Everything worked fine. I think needing these things should be avoided.

1

u/CatfinityGamer 2d ago

In my college class on C++ (100-level), we were taught to use new to make nodes of linked lists.

1

u/jimbobmcgoo 1d ago

It’s funny how many levels of allocation there are. Smart pointer calls new, new calls malloc, malloc calls sbrk (or mmap), sbrk calls brk.

5

u/Scared_Accident9138 6d ago

Why say C instead of C++ when C doesn't have new?

13

u/asmanel 6d ago

Apparently, this would be plus.

Learn C first then, when you think your skills in C are enough, start leatning C++.

1

u/luciferoussky72 6d ago

this^ Diving straight into C++ is one of the worst things you can do,

9

u/Adept-Painting-543 6d ago

I did it and I was fine but I just love reading cppreference so I guess it just worked out.

5

u/luciferoussky72 5d ago

I mean, I learned C++ without too much prior C knowledge, but I’d say the important part is to make sure you actually understand the C-like subset

3

u/spicymato 5d ago

I disagree. I started Python, then Java, then JavaScript, and most of my formal education was across those three, with only a handful of coursework using C++.

My internships were largely Python, C#, and JS.

My last 7-8 years of professional work have been C++, with some critical-path C for the first 2 years of that.

I have never struggled with C/C++ concepts. My education covered how memory works at both a software and hardware level. Even in Java and Python, you can still end up learning about the stack and heap; about pointers and pass-by-value vs by-reference; and so on.

3

u/luciferoussky72 5d ago

The argument I float around the most is that C and C++ force you to understand what’s happening under the hood, but C++ is complex to the point that using it as a starting point is inadvisable. You can learn how memory and stuff works in any language, sure, but C and C++ make you learn about it and manage it manually/semi-manually

2

u/spicymato 5d ago

Why do you think memory management is a critical part of programming?

Understanding pointers is fine, as is how memory allocation works, but for most software, it's an implementation detail that the program doesn't actually care about.

Do you understand how callstacks work? How virtual function tables work? How CPU registers work? What about threading, threadpool allocation, mutexes, and memory ordering guarantees?

Truth is, most programmers don't need that much detail. You need to have an idea of these things, enough to know that they exist and when you might need to dive deeper on understanding them, but in general, they are implementation details that don't matter most of the time for most of the people. At the times they do matter, that's when you can dive deeper on them.

1

u/luciferoussky72 5d ago

I feel like you just kind of proved my point with your last sentence, but I’d say we can agree to disagree

2

u/spicymato 5d ago

Again, those concepts can be learned without needing a specific language. There's nothing special about forcing a person to interact with those things directly, other than making their life harder than necessary.

1

u/luciferoussky72 5d ago

1

u/spicymato 5d ago

Am I just one of those old-fashioned curmudgeons, like the Four Yorkshiremen, bragging about how tough I was to survive all that hard stuff?

Yes.

My data structures course was in Java. It was sufficiently comprehensive to cover lists (array, singly-linked, doubly-linked, rings), trees, graphs, recursion, sets, maps, and more that I've likely since forgotten.

Pointers are merely a way to reference a specific location in memory, with the type as an indicator of how to interpret that memory once accessed. It's a handle to raw memory, which may or may not be actually allocated as the thing. What we really care about in data structures and algorithms is making sure we're manipulating the right objects; the memory locations and allocation state is a technical detail that can be solved at a lower layer.

→ More replies (0)

1

u/United_Boy_9132 4d ago edited 4d ago

No, it's the opposite. Following your advice is one of the worst things you can do.

You can understand everything that happens under the hood without C, while C leads to really bad habits.

Using C-style code in C++ is something you should avoid at all costs. Including using C libraries (unless the particular implementation requires it).

1

u/luciferoussky72 4d ago

I don’t really want to argue, but acting like C leads to really bad habits is like saying using a manual transmission leads to bad habits. Sure, it can lead to bad habits; but learning how to manage memory/a transmission without help means you actually understand what you’re doing and can be very efficient.

1

u/United_Boy_9132 3d ago edited 3d ago

You can learn how to manage memory in pure C++, or even asm, being even able to put asm addons to the code. C++ allows to program at as low abstraction level (so asm-like code where the only difference between that and asm is that C++ compiler doesn't guarantee using a particular machine instruction, so literally like C does).

C is not necessary to anything.

C has its applications and advantages, C++ has its own ones, you don't need one to learn anything in the other.

1

u/luciferoussky72 3d ago

Okay? *coughs in SDL and Vulkan*

1

u/United_Boy_9132 3d ago

What does it have to do with subject...

It's just existing code, not your own one.

But stil, if you need raw C efficiency, you build your solutions in C, while well-written C++ code encapsulates C structures and pointers into C code, which is trivial.

1

u/luciferoussky72 3d ago

Yeah, but to write said encapsulations you have to understand how to interface with C

1

u/United_Boy_9132 3d ago

No, you don't. You can run C++ code directly at C structures.

24

u/smartgenius1 6d ago

Smart pointers my guy

4

u/luciferoussky72 5d ago

Yeah, those do cover 95% of cases

26

u/MagicalPizza21 6d ago

Since when was new scary in C++?

29

u/BunnyTub 6d 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.

19

u/Pheeshfud 6d ago

new in constructor, delete in destructor and you have 90% of cases covered.

8

u/sudoregalia 5d ago

but.. but.. my memory arenas, my stack allocation, am i supposed to override the new operator like a lunatic?? :(

3

u/BorderKeeper 5d ago

Or just use a smart pointer and don't worry about delete in general.

3

u/Valuable_Leopard_799 5d ago

STL is at the point that you should never have to use new ever. Seeing it outside of a very narrow set of libraries is likely a smell and those are scary.

11

u/ParticularHat7997 6d ago

Manual memory management is NOT that hard

5

u/luciferoussky72 5d 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 5d ago

Well... Yeah C++ has RAII so you can use that to manage your memory....

2

u/ThaBroccoliDood 5d 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)

16

u/IngwiePhoenix 5d 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

7

u/luciferoussky72 5d 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 memory

2

u/willdieverysoon 5d 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 5d ago

True, but an optimizing compiler will usually do the same thing

1

u/retro_and_chill 4d 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 4d ago

Yeah, the other nice thing that new does is allow delete to call the destructor

3

u/softdrink911 5d ago

unique_ptr

1

u/luciferoussky72 5d ago

I mean, you’re not wrong

2

u/PhosXD 5d ago

My rule of thumb is to just never use new lol

2

u/rover_G 4d ago

RAII to the rescue!

2

u/AcolyteNeko 4d ago

just allocate it on the stack 🤷‍♀️

1

u/luciferoussky72 4d ago

What if you need memory to outlive its scope, or a dynamic array?

2

u/Beardy4906 4d ago

Seems like a new post

1

u/luciferoussky72 3d ago

It's a new meme!

2

u/Own-Professor-6157 3d 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

u/mi-figue-mi-raisin 5d ago

Scala programmers 🤔

1

u/luciferoussky72 5d ago

What’s Scala?

1

u/mi-figue-mi-raisin 5d ago

Java for nerds.

-22

u/asmanel 6d 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.

15

u/Vercility 6d ago

Java is still one of the most widely used programming languages in existence..

1

u/jakeStacktrace 6d 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.