0
Para que asignar memoria con 'new' en c++??
-¿que ventajas y desventajas hay al asignar memoria del Heap con 'new' en vez de usar el stack(declarar variables de forma tradicional y manejarlas sin usar punteros)? -mi programa será mas veloz o lento? -consumira mas o menos memoria -en que casos es necesario asignar memoria con 'new'? se agradece la ayuda :-)
4 Réponses
+ 1
If you are willing to translate this post in English, I would be glad to try to help you !
+ 1
Baptiste E. Prunier the most possible translation is this
Why allocate memory with 'new' in c ++?
-What advantages and disadvantages are there when allocating Heap memory with 'new' instead of using the stack (declare variables in a traditional way and handle them without using pointers)?
-My program will be faster or slower?
-consume more or less memory
-in which cases is it necessary to allocate memory with 'new'?
help is appreciated :-)
0
Thanks Mohit the coder (M.S.D) !
Here is my answer
- You use new to allocate a memory space which you do not know the size at compile time
- advantage: you can have the size you want at runtime, disadvantages: you need to handle it (delete) even though smart pointers help a lot on that point
- I think it is slower but I am not sure, I'll do some benchmark after this post
- I would say the same: the same size of space and one variable to store the first address, but I am not sure if static arrays are handled the same way. Worst case scenario, pointers take 8 octets more, which is most of the time nothing
- when you need a runtime defined size array
0
https://code.sololearn.com/cMLfBJb1J66h/?ref=app
Here is the bench ! Pointers are slower (that is what I thought but I was not sure)