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 :-)

5th Sep 2017, 4:07 PM
Carlos Castillo
4 Réponses
+ 1
If you are willing to translate this post in English, I would be glad to try to help you !
25th Jul 2019, 9:59 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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 :-)
25th Jul 2019, 10:01 AM
MsJ
MsJ - avatar
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
25th Jul 2019, 10:07 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
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)
25th Jul 2019, 10:27 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar