![]() |
How can I have large global variables that don't take up space in my program file? |
Previous | Memory, C Variables, and Pointers | Next |
Q: |
I would like to have a global screen buffers which need to be allocated
dynamically (to avoid wasting space in the program). The problem is that global
variables must be initialized. In your programs, you define the buffer in the
_main function, but then they are not available in the other functions. Is
there a way to make "global" non-initialized screen buffers?
|
A: |
Declare a pointer initialized to NULL, then at the beginning
allocate the buffer with malloc. For example, do this:
#include <tigcclib.h> ... void *buff; // Buffer pointer ... void _main(void) { ... buff = malloc (LCD_SIZE); // Alloc buffer, make "buff" point to it if (!buff) ... // Do some error handling (no memory) LCD_save (buff); ... LCD_restore (buff); free (buff); }Simple? |