ZealOS/src/Demo/GlobalVars.CC

77 lines
1.5 KiB
HolyC
Raw Normal View History

2020-02-15 20:01:48 +00:00
//Demonstrates dynamic initialization of vars.
//Static vars are, essentually, global vars.
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
2020-02-15 20:01:48 +00:00
to force global vars onto the data heap.
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';