API Memory: Heap or Stack? (C++) -


context: i'm making simple library. in it, i'm returning 1 of classes i've created, could, a) declare on stack , expect user copy heap if needs on heap, or, b) declare on heap , expect user delete object when they're done it.

which of these methods best practice, , if there isn't clear winner, should used where? i'm thinking i'll create .dll (.so, etc), answer change if creating different type of binary? happen if user copied headers , sources project , built entire library each time build project. change?

side note: suppose third option allow parameter signify whether or not object returned function on stack. seems convoluted, though, , i've never seen library/api that. fourth option allow pointer already-existing object, filled new data. way allows user specify sort of object they'd like, it, again, seems rather convoluted.

always prefer automatic memory management. use dynamic memory management if need dynamic lifetime. if function's purpose return t, there no need dynamic lifetime here.

if need dynamic lifetime, not ever deal raw pointers objects need delete- use smart pointers.

also,

expect user copy heap if needs on heap

you mean move heap, right?


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -