Creating an array on the heap that is not deleted

Hi there!

Currently I'm working on a c++/cli windows forms app.
What I want to do, is creating an array (with variable size)
on the (managed) heap, when button1 is pressed.
The problem is that it goes out of scope and then it gets
deleted, but I want to process the array-data when button2
is pressed, and save it when button3 is pressed.
I thought of creating it globally, but the size of the array
is variable, sometimes it needs to be 1 megabyte, sometimes
50 megabyte.
Can someone please help me with an example, or a link to a
tutorial?
Thanks in advance!!!

Derk
Very easy to do. You can use the C++ new keyword or Windows Api functions, e.g., HeapAlloc(), to allocate any amount of memory available. These allocators return a pointer to the memory upon success. This memory won't go out of scope if locally allocated, i.e., in a message handler, however, if the variable to which the memory was assigned goes out of scope, you will lose access to the memory unless you've saved the returned pointer's value to somewhere. In GUI environments in Windows that 'somewhere' is either as part of the WNDCLASSEX instance memory, or through use of the GetProp()/SetProp() functions. In OOP speak, you need to save the pointer to some object's instance data in the event handler of your first button click, then retrieve it in the other button clicks where you'll use it.

In my work, I prefer to use WNDCLASSEX::cbWndExtra bytes to save pointers, but that's just my personal preference. Of course, at some point in the app you should release the memory. Do you understand what I'm saying, or are you lost?

Oh, I see you are talking C++/Cli. That's another kind of animal. What I said above might possibly be accomplished there differently.

Last edited on
Thank you!
In c++/cli you can use the new keyword, but I read somewhere that mixing code (c++ and c++/cli) is not recommended unless you know what you are doing...
But I think I will try your solution for now.
Topic archived. No new replies allowed.