The MASM Forum
General => The Soap Box => Topic started by: jj2007 on November 17, 2022, 08:36:16 PM
-
C++ madness (https://www.quora.com/Why-does-C-have-both-new-and-malloc-Does-one-take-more-memory-than-the-other-Whats-the-difference-between-them/answer/Jerry-Coffin-2):
malloc was inherited from C. It allocates “raw” memory that hasn’t been initialized in any way to contain any actual objects. It’s entirely up to you to take the memory you allocated and initialize it in a way that make sense for the way you’re using it.
new was added in C++. It allocates objects. Those objects normally occupy some memory, so it (indirectly) allocates the memory to hold the objects as well.
But the new operator doesn’t allocate that underlying memory space itself—it uses something slightly different called operator new. Despite the similar names, “the new operator” and “operator new” are really completely different things. At least in terms of externally visible behavior, the direct analog of malloc in C++ is really operator new, not the new operator. In fact, it’s entirely legitimate to implement ::operator new something like this:
void *operator new(size_t _Size) { return malloc(_Size); }
The same is basically true on the deletion side: free just returns a block of memory back to the heap for reuse. The delete operator destroys the object(s) that were created by new, and then uses operator delete to release the underlying memory.
Although there is a form of the new operator that allows you to allocate an array of objects, I’d (strongly) advise basically treating it as if it doesn’t exist. I’m reasonably certain there’s no situation where it’s useful. Any time you think you want it, you probably want std::vector instead (and no, contrary to widespread belief, you do not use an array new to implement std::vector—you use operator new to allocate raw memory and placement new to create objects in that memory).
Then again, I’d generally advise against using the non-array form of the new operator directly either. Here there is an exception: if you’re implementing std::make_unique, (or, I suppose std::make_shared) you do pretty much need to allocate an object, so you use the new operator to do it. Most code that needs to allocate an object dynamically should use std::make_unique though (and a little needs std::make_shared, but it’s much less common).
I love Assembly :tongue:
-
:rofl: :tongue:
-
What a great comedy routine. Someone should read the above quote with comedic sound effects. :joking:
-
What a great comedy routine. Someone should read the above quote with comedic sound effects. :joking:
You could use mimimum cpp classes in windows only register window class
Lots of gdi objects,without delete objects can after a while stop working because memory leakes
-
Just come up question, if I first load bitmap resource from file use it in main program
A different separate program writes new version of bitmap and sends message to main program to update = load bitmap resource again
I wanna do it the way of prevent memory leaks
So delete or deleteobject first before load resource again?
When I Work with make art/animation the above workflow is common
Example Photoshop 2d editing textures opened beside 3d app, when it doesn't look right in 3d app,return editing 2d texture and update/reload texture
-
So delete or deleteobject first before load resource again?
Yes.
The application must call the DeleteObject function to delete each bitmap handle returned by the LoadBitmap function.
:thumbsup:
-
Alloc & Free 1000*10MB in Assembly:
include \masm32\MasmBasic\MasmBasic.inc
Init
push 999 ; one thousand loops
.Repeat
mov ecx, 10000000 ; ten Million
Let edi=New$(ecx) ; MbAlloc ;-)
push edi
mov al, "x"
rep stosb ; fill the buffer
pop edi
Clr$ edi ; free the buffer
Print "*"
dec stack
.Until Sign?
pop edx
Inkey cfm$("\nall is fine")
EndOfCode
-
From the popular series "Have fun with C++":
Why are the ‘new’ and ‘delete’ keywords considered bad in modern C++? (https://www.quora.com/Why-are-the-%E2%80%98new%E2%80%99-and-%E2%80%98delete%E2%80%99-keywords-considered-bad-in-modern-C++)
As User-11956034714365873629 alludes to, the idea is that since C++11, new and delete should only be used in library code. Specifically, in almost all cases where you would want to create a single object with new, you should instead be using std::make_unique (or, if you don’t have C++14 support yet, a library function that does the same thing as std::make_unique), and for multiple objects a standard library container such as std::vector, which may be moved at will, is usually appropriate. In the few cases where you need to have shared ownership with reference counting, you could use std::make_shared.
The reason for this is that every time you write new in application code, you are creating an unmanaged object on which delete must be called at some later point unless you want a resource leak [1]. The smart pointer and standard library container classes make it easy to ensure that every new will be paired with exactly one delete [2] and are applicable to almost all situations in which you would want to use dynamic allocation. If you need some unusual memory management logic, you should still incorporate it into an RAII class so that the language helps you ensure the destructor will be called, releasing the resources held; this will make it much easier to write correct code than using raw new and delete.
Due to exception safety considerations, it is better to not use raw new even when it is intended for ownership of the pointer returned to be taken immediately. For example, the following code can leak if new Foo is executed first, then, before the std::unique_ptr<Foo> is constructed, the new Bar expression is executed and fails due to an exception; in that case the Foo object will not be destroyed. Using std::make_unique prevents this issue.
-
Yeah, only reason to use C and C++ is that, because Windows API Documentation is made using C and C++ :dazzled:
-
Isnt the good programming habits vs bad programming habits according to cpp programmers aimed at library making, for example use much local variables in functions (procs) ?
And moduls easy to Include in another cpp program?
Example Every wm_paint create and delete objects, one forgotten deleteobject sooner or later big memory leak ?
Isnt it also bad habit to simple "new" in text editor for any proffesional programmers, better productivity start from template with at least few % coding already made ?
@c3
Oop part of cpp makes it easier to replace the car object programmer that produces buggy car object beyond deadline with new programmer
Exotic undocummented asm code that make you irreplacable, unwanted in a Company