Yes, this question has been asked before. I haven’t found a good search phrase for an answer. First an example in C…
malloc()/free() is used on pmsg in the mangled code below so that pmsg survives calls to foo(). I would like to do the equivalent in Rust.
LRESULT def foo(UINT message) {
static int line_count ;
static MSG *pmsg = NULL ;
int i;
switch (message) {
case WM_CREATE:
case WM_DISPLAYCHANGE:
line_count = GetSystemMetrics (SM_CYMAXIMIZED) ;
if (pmsg)
free (pmsg) ;
pmsg = malloc (line_count * sizeof (MSG)) ;
return 0 ;
case WM_KEYDOWN: // Rearrange storage array and store new message
for (i = line_count - 1 ; i > 0 ; i--) {
pmsg[i] = pmsg[i - 1] ;
}
pmsg[0].message = message
break ;
case WM_PAINT: // do something with pmsg
...
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
Somehow I’d like to create something like
static mut MSG_VEC: Vec<MSG> = Vec::new();
within a function so that MSG_VEC survives between function calls. Can someone suggest how?
If this is not possible then maybe I need to emulate the C code more closely and use really unsafe code with raw pointers + malloc/free. Not necessarily with the unfreed pmsg on program exit! How do I do that?