Skip to main content
gamedev.net
🔒 Locked

C++: dynamic array visibility

Started by Alessandro Aug 26, 2006 at 4:29 PM 6 replies 750+ views
Original Post
Alessandro
Alessandro
I've this problem with dinmaic arrays: i declare the following array in the init() of my application: int rows=GetRows(); int columns=GetColumns(); double *DEMDATA = new double[rows*columns]; The array is filled up with values and i'd like to use it in another function, but i can't because it's not declared as a global. Thanks for any help, C++ drives me nut!
Zakwayda
Zakwayda
C++ variables are only visible in the scope in which they're declared (more or less). If you want variable X to be visible to functions Y and Z, then it needs to be declared at an appropriate scope (class, file, global, etc.).
biscuitz
biscuitz
You could pass a pointer to the array to the function that you want to share the array with. There are several ways of passing variables between functions. Can't really help much without more information.
Alessandro
Alessandro
Sorry, i will try to explain it better:

The init() function is called only once at startup. It read the variables rows and columns from an external file and create the array

void init(void){int rows=GetRows();int columns=GetColumns();double *DEMDATA = new double[rows*columns];}


In the main application loop, i call the Render() function which should read the DEMDATA array and draw a triangle strip. However the DEMDATA array is not accessible because it's not global, so my question is if there is a way to achieve this with dynamic arrays, since their size is determined at runtime

void Render(void){for (int i=0; i<(row*columns); i++) {  renderstrip(DEMDATA); }}



Please tell me if my question is still unclear.
Zakwayda
Zakwayda
Quote:
Original post by Alessandro
...so my question is if there is a way to achieve this with dynamic arrays, since their size is determined at runtime
Whether the size of an array is determined at run or compile time is unrelated to its scope. In your example it looks like your functions are global, so DEMDATA should be global as well. If the entire program is in one file you could put DEMDATA at file scope, else you could use the extern keyword to make it available to any file that needs it.

Note that these aren't necessarily the best solutions from a software design perspective, but the first step is probably to resolve the scope issue.
biscuitz
biscuitz
Eh... okay you said that DEMDATA array is not a global. But inside the init function you're defining variables and not doing anything with them. Basically init isn't doing anything unless those variables it's assigning are globals, which they're obviously not because you've declared them inside the function with datatypes int and double.

I guess the code should look something like this:
int rows;int columns;double * DEMDATA;void init(void){rows=GetRows();columns=GetColumns();DEMDATA = new double[rows*columns];}

void Render(void){for (int i=0; i<(rows*columns); i++) {  renderstrip(DEMDATA); }}
quack
quack
Theres no issue with dynamic arrays or anything. Jyk mentions scope and that's pretty much what the problem boils too, here is one example of scope. most of the times anytime you use the {} the variables declared in it are only availible within those brackets.
1: namespace global2: {3:  int a; //a declared in above bracket "{"4:5:  int function1()6:  {7:    int b; //b declared in above bracket "{"8:    { 9:       int c; //c declared in above bracket "{"10:       a = 3; // ok as a is declared in the global brackets line 3:11:       b = 3; // ok as b is declared in the function brackets 7:12:       c = 3; // ok as c are in the current brackets13:    } // bracket closed c nolonger availible14:    a = 3; // ok15:    b = 3; // ok16:    c = 3; // ERROR c was only availible in the above brackets17:  }// bracket closed b nolonger availible18:19:  void function2()20:  {21:    a = 3; // ok as a declared within global brackets line 3:22:    b = 3; // ERROR b not declared in any local brackets23:    c = 3; // ERROR c ""24:  }25:}[\source]
www.autodefrost.com/~travisSignature Virus cleaned by McAfee

Topic Locked

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

Sign in to reply to this topic.