ZealOS/src/Demo/GlobalVars.CC

77 lines
1.5 KiB
HolyC
Raw Normal View History

//Demonstrates dynamic initialization of variables.
//Static variables are, essentually, global variables.
2020-02-15 20:01:48 +00:00
class Test
{
I32 time;
U8 name[8];
2020-02-15 20:01:48 +00:00
};
Test g1[] = {
{10, "Name1"},
{(tS % 10.0) * 100, "Name2"}, //Dynamic initialization
{30, "Name3"}
2020-02-15 20:01:48 +00:00
};
D(g1, sizeof(g1));
"Time 1:%d\n", g1[1].time;
2020-02-15 20:01:48 +00:00
U0 Main1()
{
static Test s1[] = {
{10, "Static1"},
{(tS % 10.0) * 100, "Static2"}, //Dynamic initialization
{30, "Static3"}
};
D(s1, sizeof(s1));
"Time 2:%d\n", s1[1].time;
2020-02-15 20:01:48 +00:00
}
Main1;
/*Now, we'll use the data heap global option
to force global variables onto the data heap.
2020-02-15 20:01:48 +00:00
You can turn the data heap flag
on and off within your programs, leaving
ones which need initialization on the code heap.
You can't dynamically initialize data heap
globals--they are consts. This might be a silly
2020-02-15 20:01:48 +00:00
point, but might res in odd differences, perhaps
from the order things are evaluated.
Data heap globals are good for AOT modules
2020-02-15 20:01:48 +00:00
because they don't take-up room in the .BIN file.
*/
#ifjit
#exe {Option(OPTf_GLOBALS_ON_DATA_HEAP, ON);};
2020-02-15 20:01:48 +00:00
Test g2[] = {
{10, "name1"},
{(tS % 10.0) * 100, "name2"}, //No dynamic initialization--converted to const
{30, "name3"}
2020-02-15 20:01:48 +00:00
};
D(g2, sizeof(g2));
"Time 3:%d\n", g2[1].time;
2020-02-15 20:01:48 +00:00
U0 Main2()
{
static Test s2[] = {
{10, "static1"},
{(tS % 10.0) * 100, "static2"}, //No dynamic initialization--converted to const
{30, "static3"}
};
D(s2, sizeof(s2));
"Time 4:%d\n", s2[1].time;
2020-02-15 20:01:48 +00:00
}
Main2;
#exe {Option(OPTf_GLOBALS_ON_DATA_HEAP, ON);};
2020-02-15 20:01:48 +00:00
#endif
'\n';