Archived

This topic is now archived and is closed to further replies.

-/= Static -=electrisity=\_-

This topic is 6008 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was wondering how do you use the static and why? In Pascal or VB it somehow lets you use some of the computer''s allocated memory instead of the given amount of DOS''s 64K. (Or is it the other way around.) Anyway if I''m wrong please inform me, and please tell me how to use the static and how it''s usefull for. Thanks ------------------------------ -Last Attacker ICQ: 120585863 E-mail: laextr@icqmail.com

Share this post


Link to post
Share on other sites
Static in regards to data means that the memory is allocated only once and when you get to the place to use it again it will have the same value as the last time you accessed it even if the scope of the declaration was exited. I know that probably sounds alittle ambiguous so let me use an example:

    
int getData()
{
int i = 0; // this line will execute each time called

i++;
return i;
}
int getStaticData()
{
static int i = 0; // this line will execute exactly one time

i++;
return i;
}

int main( int argc, char *argv[] )
{
printf("Starting getData\n");
for (int a = 0; a < 10; a++)
printf("%d\n", getData();

printf("Starting getStaticData\n");
for (int a = 0; a < 10; a++)
printf("%d\n", getData();
return 0;
}


The output of this should be something like:
Starting getData
1
1
1
1
1
1
1
1
1
1
Starting getStaticData
1
2
3
4
5
6
7
8
9
10

The first function will initialize i every time it is executed, while the second will only do it once.

Seeya
Krippy



Edited by - krippy2k on June 30, 2001 7:27:34 PM

Share this post


Link to post
Share on other sites