Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
Is it possible for a program to run and create new variables on the fly? For instance: for(int NUM = 0; NUM < 10; NUM++) Above is written in C++/java format (and it wont compile to my knowledge) but do you kind of understand the idea of what I would like to do? It is just something I have been debating about, because moving over to arrays (though it may be easier) has its limits. Any ideas? |
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
well, I'm probably thinking it java, but I think that variably you created only exists inside the for loop and if you try calling it outside, it won't register. what does your compiler say? ------------------ |
D-SIPL![]() Moderator Posts: 1345 From: Maesteg, Wales Registered: 07-21-2001 |
quote: Do you mean self creating variables??? --D-SIPL ------------------ |
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Your code above is valid, and will compile. But Archangel is correct, the variable "variableNUM" will only have scope inside of the loop. It is created on the stack at the start of the loop (at the open { bracket), and popped off the stack at the end of the loop (at the close } bracket). You won't be able to use it outside of the loop.
quote: I'm not sure what you mean here. BTW, kudo's to you for getting some great coding threads started!!!! |
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
quote: Yeah! thats what I mean!
quote: Thanks! |
HanClinto![]() Administrator Posts: 1828 From: Indiana Registered: 10-11-2004 |
I think he's talking about having it create variables variable1 If that's what you're talking about, no, you can't do that. Unfortunately, you have to use arrays for that. Yes, arrays do have their limitations, but at least you can have dynamic arrays in C++ (and I think in Java also). C# is a different story, but it's still possible. Perhaps we can answer some of your questions about how to help you use arrays easier? ------------------ |
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
quote: They're called "vector"s and yes, Java has them. In both languages, you can also create objects dynamically. ------------------ Switch Mayhem now available! Get it here |
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Ah, I understand what he was asking now, thanks HanClinto. Wacko4X, sorry if my earlier answer confused you. HanClinto's answer is correct. You can use arrays, dynamically allocated memory, etc. instead. |
ArchAngel Member Posts: 3450 From: SV, CA, USA Registered: 01-29-2002 |
ArrayLists are dynamically growing, or you can use the regular Array and have a check when the array becomes close to full, copy it over to a new array that you created to be bigger. ------------------ |
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
So how can you dynamically create arrays past the simple: int a[100]; //C++ Are there special commands or something? [This message has been edited by Wacko4X (edited February 10, 2005).] |
Klumsy![]() Administrator Posts: 1061 From: Port Angeles, WA, USA Registered: 10-25-2001 |
ArrayLists and other such collections are more like a nice class implementing a linked list. as for int a[100]; //C++ in C++ that is statically allocating 100 ints and only 100 ints.. ------------------ |
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: In C++: int num = 5000; // do whatever delete [] a; // the a array is freed from memory [This message has been edited by BrianT (edited February 11, 2005).] |
Wacko4X![]() Member Posts: 92 From: Bellvue, WA, USA Registered: 08-21-2002 |
quote: So in this example: Is that right? Now, how would you do it in Java? |
Briant![]() Member Posts: 742 From: Stony Plain, Alberta, Canada Registered: 01-20-2001 |
quote: Ya, but I did it that way just to show you that the actual allocation (new int[num]) can be based on a variable, and not a hard-coded number. For example, you could get the number from the user, from a file, from a calculation, whatever.
quote: Yes. Technically it points to the first integer in the array, but you can index it like it was the array itself ("a[123]" instead of "*(a + 123)")
quote: Once it is deleted, yes - sort of like a normal array going out of scope. However, You can reuse the a pointer though to allocate another array, or whatever.
quote: I have no idea. |
CobraA1![]() Member Posts: 926 From: MN Registered: 02-19-2001 |
quote: int num = 5000; // do whatever // a is put onto the garbage collector, which will free it when appropriate Note that Java keeps track of all of its objects, and will get rid of them when the program closes - the "a=null" statement to delete a is totally optional. But if you expect the program to be running for long periods of time, it is highly recommended that unused references be set to null so the objects can be deleted when they're no longer used. Oh, yeah: Do not do the "set to null" thing in C++. C++ does not automatically delete objects - setting them to null will cause memory leaks, because they never get deleted! ------------------ Switch Mayhem now available! Get it here [This message has been edited by CobraA1 (edited February 12, 2005).] |
CoolJ![]() Member Posts: 354 From: ny Registered: 07-11-2004 |
You could use a vector array as well. a vector is just an array with cool functions built in. I wish I would have discovered these earlier. #include <vector> int NUM = 10; //create array of 10 integers initialized to a value of 99 // cool things you can do with vector array... //how many integers are in the array //is the array empty? // sort the integers in the array
|