Skip to main content
gamedev.net
🔒 Locked

globals from within a function

Started by James Sioutis Mar 4, 2004 at 3:41 AM 4 replies 300+ views
Original Post
James Sioutis
James Sioutis
hey is it possible to create a global variable from inside a function?? ~aussie
[~aussie]
Qw3r7yU10p!
Qw3r7yU10p!
yes but you'll need to either

1) return it from a function or
2) set some other variable somewhere else that knows how to access it.


//1)

int* getGlobalInteger() {
static int globalInteger;
return &globalInteger
}

//2)

void someFunction() {
static int globalInteger;
static bool firstTimeThrough = true;
if(firstTimeThrough) {
tellSomeOtherFunctionAboutThisGlobal(&globalInteger);
firstTimeThrough = false;
}
}


There's probably a number of other things you could do.

[edited by - petewood on March 4, 2004 4:51:25 AM]
civguy
civguy
I'm not sure if this is what you mean:

int test() {
static int x = 0;
return ++x;
}

The x inside test() isn't globally accessible, i.e. you can't refer to it from outside test, but it's global in the sense that it preserves information between calls to the function, i.e. globally. If you call test() repeatedly, it will return consecutive numbers 1,2,3,4,5,6... even though it doesn't rely on outside globals or parameter value.

But one can't define globally accessible global inside a function. edit: unless you return the static variable's address like petewood did above.. Many singleton implementations do that.

[edited by - civguy on March 4, 2004 4:57:17 AM]
James Sioutis
James Sioutis
hey thanks for your reply, what im doing is re-writing quadtree code i made a while ago, and i have 2 globals:

float *terrain = new float[...];
float *normals = new float[...];

In order for the other functions to access these vars, i have to make them global, but i dont know what size they are untill the Init() function. so if i have somethng like this:

void CreateQuadTree(quadtree *node, int maxSubdivisions, float mapSize, float *mapData)
{
float terrain = new float[mapSize]; //i have to make this var global so the other funcs can access it

//do some other stuff
}

i dont think i wrote that too well but thats basically what my problem is

~aussie
[~aussie]
Qw3r7yU10p!
Qw3r7yU10p!
if you''re using c++ why don''t you make a class?

and what if you have two quad trees?

globals on the whole are pretty much unnecessary. Pass around any data that is needed. Also use std::vector instead of a plain C-style array as you can ask it for its size. Pass it around by reference. You won''t need to pass the size parameter. Encapsulate. Make classes.
James Sioutis
James Sioutis
i already do, i have a quadtree struct looking somethng like this:

struct quadtree
{
float *pVertices;

//some other stuff

quadtree *nodes[4];
};

thats kinda what it looks like, i had already tried passing in the data to the render function instead of making it global, but because it recurses up to 100 times it slowed it down.
Im already writing it all again and trying something else so ill see if this works or not, thanks for your help

~aussie
[~aussie]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.