Game Programming and Development Tools

Dynamic creation of Variables – Wacko4X

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++)
{
int variableNUM = 99;
}

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?

------------------
Soterion Studios

D-SIPL

Moderator

Posts: 1345
From: Maesteg, Wales
Registered: 07-21-2001
quote:
Originally posted by Wacko4X:
Is it possible for a program to run and create new variables on the fly?

For instance:

for(int NUM = 0; NUM < 10; NUM++)
{
int variableNUM = 99;
}

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?


Do you mean self creating variables???

--D-SIPL

------------------
If at first you don't succeed, destroy all evidence that suggests you tried

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:
Originally posted by Wacko4X:
[B]Is it possible for a program to run and create new variables on the fly?

For instance:

for(int NUM = 0; NUM < 10; NUM++)
{
int variableNUM = 99;
}

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?


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:

It is just something I have been debating about, because moving over to arrays (though it may be easier) has its limits.

I'm not sure what you mean here.

BTW, kudo's to you for getting some great coding threads started!!!!

------------------
Brian

Wacko4X

Member

Posts: 92
From: Bellvue, WA, USA
Registered: 08-21-2002
quote:
Originally posted by D-SIPL:
Do you mean self creating variables???

--D-SIPL


Yeah! thats what I mean!

quote:
Originally posted by BrianT:
BTW, kudo's to you for getting some great coding threads started!!!!

Thanks!

HanClinto

Administrator

Posts: 1828
From: Indiana
Registered: 10-11-2004
I think he's talking about having it create variables

variable1
variable2
variable3
variable4, etc, by substituting NUM in there for it.

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?

------------------
http://www.includingjudas.com/christiangame.html

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
but at least you can have dynamic arrays in C++ (and I think in Java also).

They're called "vector"s and yes, Java has them.

In both languages, you can also create objects dynamically.

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:
Originally posted by HanClinto:
I think he's talking about having it create variables

variable1
variable2
variable3
variable4, etc, by substituting NUM in there for it.

If that's what you're talking about, no, you can't do that. Unfortunately, you have to use arrays for that.


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.

------------------
Brian

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.

------------------
Soterion Studios

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++
int a[] = new int[100]; //Java

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++
int a[] = new int[100]; //Java

in C++ that is statically allocating 100 ints and only 100 ints..
you couldn't do
int a[x] where X is a variable.. to do that you rather make a pointer to an array
however in java it could be *i think*
int a[] = new int[x] and thus X can be whatever it happens to be at RUN time.. because in java.. a is REALLY a pointer to an array (or called a reference), its just that the pointer mechanism is HIDDEN from the programmer, and the programmer isn't allowed to take over and do funky pointer maths themselves.


------------------
Karl /GODCENTRIC
Visionary Media
the creative submitted to the divine.
Husband of my amazing wife Aleshia
Klumsy@xtra.co.nz

Briant

Member

Posts: 742
From: Stony Plain, Alberta, Canada
Registered: 01-20-2001
quote:
Originally posted by Wacko4X:
So how can you dynamically create arrays past the simple:

int a[100]; //C++
int a[] = new int[100]; //Java

Are there special commands or something?


In C++:

int num = 5000;
int *a;
a = new int [num]; // a is now an array of 5000 ints, allocated on the heap

// do whatever
a[0] = 55;
a[500] = 777;

delete [] a; // the a array is freed from memory

------------------
Brian

[This message has been edited by BrianT (edited February 11, 2005).]

Wacko4X

Member

Posts: 92
From: Bellvue, WA, USA
Registered: 08-21-2002
quote:
Originally posted by BrianT:
In C++:

int num = 5000;
int *a;
a = new int [num]; // a is now an array of 5000 ints, allocated on the heap

// do whatever
a[0] = 55;
a[500] = 777;

delete [] a; // the a array is freed from memory


So in this example:
-num still equals 5000
-a is still a pointer to an integer
-everything else (the allocation of memory for the array and the array itself) is deleted...gone forever

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:
Originally posted by Wacko4X:
So in this example:
-num still equals 5000

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:

-a is still a pointer to an integer

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:

-everything else (the allocation of memory for the array and the array itself) is deleted...gone forever

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:

Now, how would you do it in Java?

I have no idea.

------------------
Brian

CobraA1

Member

Posts: 926
From: MN
Registered: 02-19-2001
quote:
Now, how would you do it in Java?

int num = 5000;
int[] a = new int[num];

// do whatever
a[0] = 55;
a[500] = 777;

// a is put onto the garbage collector, which will free it when appropriate
a = null;

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!

------------------
"The very idea of freedom presupposes some objective moral law which overarches rulers and ruled alike." -- C. S. Lewis (1898 - 1963), "The Poison of Subjectivism" (from Christian Reflections; p. 108)

Switch Mayhem now available! Get it here
Codename: Roler - hoping to get more done over the holidays . . .

[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>
using namespace std;

int NUM = 10;

//create array of 10 integers initialized to a value of 99
vector<int> a(NUM,99);

// cool things you can do with vector array...

//how many integers are in the array
int size = a.size();

//is the array empty?
bool = a.empty();

// sort the integers in the array
sort(a.begin(),a.end());


//accessing the integers with []
int myint = a[5];


vectors can be of any type - string, floats, longs, or your own structs, whatever.