Making a staic variable dynamic?

Started by
10 comments, last by Boooke 12 years ago
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?
Advertisement
Could you post the relevant code and your goal ?
I will do so when I get to my "source" computer :-)

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.
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

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).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
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.
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.
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.
Just move the declaration of users to outside the if-statment.

As suggested by SimonForsman

This topic is closed to new replies.

Advertisement