Jump to content
  • Advertisement
Sign in to follow this  
Boooke

Making a staic variable dynamic?

This topic is 2276 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 have a struct array created inside an if-statement. The problem is, I guess it will be destroyed when the loop ends, which it will do right after the creation of this struct. As I see it, making it static could be a solution. But the struct needs to be changed and altered often, so making it static might not help at all, since it could not be dynamic. Is there a solution to this, or do I have to split my program into functions inside the if/else statement?

Share this post


Link to post
Share on other sites
Advertisement

so making it static might not help at all, since it could not be dynamic.



I think you are misunderstanding what static means/does. It sounds like you are confusing it with const(ant).


When you make a variable static, you are effectively making it into a global that can only be used within the scope it is created. The two biggest traits of a static variable are a) there can be only one b) they are created/allocated when your program starts, not when they are first encountered. The value of a static variable can be changed just like any other variable.


That said, I think there is a better solution that using a static, it really comes down to seeing your code.

Share this post


Link to post
Share on other sites
Hi you say that you are using an array could you not use a vector or something instead that way you can empty it and fill it up as you need also you can make the vector static

Share this post


Link to post
Share on other sites

Hey, I have a struct array created inside an if-statement. The problem is, I guess it will be destroyed when the loop ends, which it will do right after the creation of this struct. As I see it, making it static could be a solution. But the struct needs to be changed and altered often, so making it static might not help at all, since it could not be dynamic. Is there a solution to this, or do I have to split my program into functions inside the if/else statement?


Declare the array in the scope in which it is used, (If you need it to persist between iterations of the loop you should declare it just outside the loop), Stack allocated variables are freed when they go out of scope. (Heap allocated data will not be freed automatically but the pointers you use to access them can still go out of scope making the memory inaccessible (and unfreeable which is a memory leak).

Share this post


Link to post
Share on other sites
Hello, and thank you for all the answers. Yes, I realize there is a difference between constants and statics, but I was just not sure if a static variable was, well, variable (As in, changing the values).

My code is a mess so here is the relevant code:

inFile.open("userlist.txt");
if (!inFile.good())
{
cout << "No user file, creating new.";
outFile.open("userlist.txt");
// outFile.write("0\n",2);
outFile.close();
inFile.open("userlist.txt");
userlist * users = new userlist;
}
else
{
// Here i will read the userlist into the structure, since it do exist
}


where 'users' is a userlist-structure. This structure should be preserved

But yes, I thought of just making a vector instead. I just don't have any experience with them. But now I see, that is the most clean solution - at least to me - since I have to add and remove elements from the structure vector/array. With arrays I guess it would require copying the old array to a new one and then delete it. I am creating an "admin" sort-of user as the first vector element, but I haven't had time to code much, so it is not part of the source.

Share this post


Link to post
Share on other sites
It's a bit hard to suggest what to use since we don't really know what you're going to be doing with the data but using a vector or a list or something is definitely preferable to just an array if you're going to be adding/removing data constantly.

Share this post


Link to post
Share on other sites
Well, I can and try explain it. The structure will need to contain all sorts of user information there is, either created inside that loop (If "userlist.txt" do no exist)or read from a file (If "userlist.txt" exists). This data will, probably not be used for anything useful, but it is a requirement of this assigment I have. I will probably only use the "ID" and "name", which are string variables inside of the structure to sort the data and read messages from- and to users. Users will be deleted from time to time, and it should be resettable. I think it boils down to adding, reading, and deleting elements of the vector structure, and sometimes removing it completely. I haven't written much of this code yet.

I will look into vectors, and replace the array.

Share this post


Link to post
Share on other sites
Just move the declaration of users to outside the if-statment.

As suggested by SimonForsman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!