Question

I would like to have a variable to persist even after program termination. In other words, I'd like for a variable to remain in the memory even after an application exits from the main function. So, if my application is launched again, it could access that variable directly from the memory. Is this even possible? Would dynamic allocation, e.g. array=new int[size], do the trick?

Was it helpful?

Solution

No, all memory is reclaimed by the os on process termination. You have to save stuff you want to a file.

OTHER TIPS

It's not possible. You have to store the data in a file or system preferences in order to access it on the next launch

Disclaimer: Storing the values to a file or using some framework functionality like QSettings should be preferred over the following approaches.

If You really want Your variables to remain in the memory and if You can risk to loose the values in a reboot, then ask the operating system for shared memory.

If You have a POSIX-compliant platform like Linux or Windows, then use the POSIX functions. To quote the manual:

POSIX shared memory objects have kernel persistence: a shared memory object will exist until the system is shut down, or until all processes have unmapped the object and it has been deleted with shm_unlink.

Be warned, that this introduces a kind of memory leak: Your variables will consume memory even after the termination of the application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top