Sign in to follow this  
Boooke

Making a staic variable dynamic?

Recommended Posts

Boooke    104
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
Serapth    6671
[quote name='Boooke' timestamp='1333019666' post='4926304']
so making it static might not help at all, since it could not be dynamic.
[/quote]


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
thestien    102
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
SimonForsman    7642
[quote name='Boooke' timestamp='1333019666' post='4926304']
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?
[/quote]

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
Boooke    104
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:
[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
}
[/CODE]

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
Satharis    2443
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
Boooke    104
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
way2lazy2care    790
[quote name='Olof Hedman' timestamp='1333097271' post='4926609']
Just move the declaration of users to outside the if-statment.

As suggested by SimonForsman
[/quote]
1. Make a UserList class that deals with managing and storing the userlist. Pretty much it would just be a wrapper for a vector of users. Mostly I would do this to keep the logic dealing with the list in one place.
2. Put the UserList in the scope you will need the data, and pass the reference to it into your populate function to fill the list.
3. Enjoy life.

Share this post


Link to post
Share on other sites
Boooke    104
Hello, and sorry for the later answer. I am currently trying to replace the array with vectors, and I have created a vector inside of a class to contain the structure types. This class should contain all the functions required to use the structure. I am not really sure how to use the structure vector yet, and how to access members, but I am trying to read through different references to understand it better. Thank you for the replies. I think I can work something out now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this