Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
azuredivay1088247dI dont get it, whats different/strange here? .-.
vector.data() will give the pointer to what you just initialised, delete will clear the data it points to, so after this you should be left with an empty vector
if im not mistaken -
azuredivay1088247dAHH u mean, the vector wont be pointing to anything? is that the joke? ._.
coz delete would deallocate the memory assigned for its data? -
Lensflare17160247dI guess the joke is that delete is for stuff allocated on heap with "new", but the thing that is deleted here is allocated on the stack.
Thanks for the C++ PTSD flashbacks, btw! -
Lensflare17160247dAlso, what kind of maniac sets his IDE to show whitespace as little dots? It‘s honestly making me aggressive just by looking at it! 😂
-
CoreFusionX3423247d@azuredivay
Of course it's gonna compile.
The delete will only free the memory for the first element, but it will likely crash anyway when that vector goes out of scope and its destructor tries to free an already partially freed block...
Or!
Fun shenanigans could happen if something else allocates an int in the heap and it gets that same address... -
azuredivay1088247d@CoreFusionX really? .-.
isnt it sequential memory block? as long as the 1st thing it's pointing to is null (which is what Delete would do here) wont C++ ignore the rest? i.e., the "someData" would hold a null pointer, which isnt a rare occurrence
And if the address space is assigned to something else, it'll overwrite it so I dont see it causing any big issue
At this point it's faster to type it n test eh -.- -
CoreFusionX3423247d@azuredivay
So, by all means, do.
Some misconceptions need clearing up first, though.
Bear in mind much of this stuff is implementation dependent, and when so, I'll mark it with (ID).
Is vector a sequential block of memory? No. A vector has a stack part (usually three pointers, ID), and an allocation in the heap for its contents.
Does delete set the deleted pointer to null? No. For all it cares, it *could*, but it's (ID) and most don't. You also have the aliasing problem to contend with.
data() returns a pointer to the beginning of the heap allocated storage, but how this storage is allocated is (ID), and can even be custom if you have a custom allocator!.
malloc/free, new/delete and new[]/delete[] each do their own (ID) bookkeeping, and mixing them is undefined behaviour.
Double deletion, which would happen when the vector went out of scope, is undefined behaviour. -
CoreFusionX3423247dAnd as always, when we are in UBland, *pray* that you get a crash, cuz that's best case scenario :).
What exactly happens in this scenario is very ID, but from what I know of most STLs, default allocator is just operator new, in which case, the whole vector storage would be freed, but should you do it in say, data() + 2, chaos will ensue.
Moral of the story: Fun academic discussion, but do not try this at home! -
Lensflare17160247d@CoreFusionX This is fascinating and terrifying.
The amount of bullshit that you need to know and keep in mind about memory management in C++ is absurd! -
CoreFusionX3423247d@Lensflare
Not really. All you gotta know it's that if you want an array-like container, use std::vector, and if you need a pointer to something just use std::shared/unique/weak_ptr (depending on how you wanna deal with ownership) and call it a day.
Modern C++ devs should never use new/delete.
It just so happens that many C++ devs didn't get past the "C" part XD. -
CoreFusionX3423247dAll in all, the bottom line is *know what you are doing*.
Even if you run with a language provided safety net, your code will always be better if you know what you are doing, and being able to go elbow deep in these things is just another tool in your belt.
As I always say, learning to drive with a MT lets you drive an AT later. The other way around... Heaps of fun... And flaming metal.
Examples of usefulness of this kind of knowledge come in stuff like small string optimization and other such compiler/STL tricks that give C++ the same speed as C while actually being safe if you aren't a dumbass, but yeah, they are aimed to be transparent to your everyday dev. -
Demolishun34918246d@CoreFusionX
"Modern C++ devs should never use new/delete."
I don't know how you could use Qt that way. -
Lensflare17160246d@CoreFusionX
My last contact with C++ was before the time that std::shared/unique/weak_ptr was added officially. I think it was before C++11.
And back then, new and delete was the standard. It’s crazy how a language feature that seems so fundamental can completely be replaced by a standard lib level feature. -
Demolishun34918246d@Lensflare so I was curious about a "modern" c++ project. I figure a well tested and modern game engine like Godot. Well, they have completely redefined new/delete and have custom allocator macros too. These all use malloc and free. I am guessing there are very good reasons to do this, but it certainly isn't getting rid of new and delete. They support multiple platforms including phones. It seems it has ref counting built into the allocators. Not sure how that is being used. Sure is interesting though.
-
CoreFusionX3423246d@Demolishun
Game engines tend to use custom allocators, particularly in ECS systems to ensure all entities of a given type are allocated next to each other, to promote cache locality, and to prevent actual allocations (which are mighty slow) mainly. -
CoreFusionX3423246d@Lensflare
It's the burden of C++.
In order to maintain ABI compatibility, they must keep many things that shouldn't be used nowadays, that's why more and more things move towards the STL with each new standard.
(And the STL implementations are behemoths of buried brilliance)
Related Rants
-
xjose97x19Just saw a variable in C named like this: long time_ago; //in a galaxy far away I laughed no stop.
-
linuxxx32*client calls in* Me: good morning, how can I help you? Client: my ip is blocked, could you unblock it for m...
-
DRSDavidSoft28Found this in our codebase, apparently one of my co-workers had written this
Really? Do you have what it takes?
Yes, it does compile.
joke/meme
wtf
brogramming
c++