• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Variable Variables in C++

Im trying to have a variable name, depending on the value of a counter. Something like this:

int i;
for (i=1; i<=10; i++)
{
int vari = i;
}

This way, at the end of the loop, i would have the following variables:

var1 = 1
var2 = 2
var3 = 3
...
var10 = 10

How would I do this???
 
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.
 
Originally posted by: cyberphant0m
Im trying to have a variable name, depending on the value of a counter. Something like this:

int i;
for (i=1; i<=10; i++)
{
int vari = i;
}

This way, at the end of the loop, i would have the following variables:

var1 = 1
var2 = 2
var3 = 3
...
var10 = 10

How would I do this???


C++ doesn't work that way 🙂 If you can tell us why you're trying to do this, then perhaps we can tell you how to accomplish it using the C++ way.
 
Originally posted by: Smilin
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.

You only have to worry about zero-based indeces when you access array items (i.e. the first item is var[0], second item is var[1], last item is var[9]), the number you put in the declaration is the number of items. So:

int i, var[10];

for(i=0; i<10; i++)
var[ i ] = i+1;
 
Originally posted by: BingBongWongFooey
Originally posted by: Smilin
forgive the newbie answer, but:

Why not just use an array?

int var[11]; (arrays start at 0 in C++ methinks?)
int i;
for (i=1; i<=10; i++)
{
var[ i ] = i;
}

You'll get

var[1]=1;
var[2]=2;
..

Otherwise I think you're going to have to use some monstrous pre-compiler directives or something.

(someone might correct my syntax...I know a ton of languages but I'm sooo rusty on all of them and tend to intermingle syntax).

edit: hehe tought to type var [ i ] without anandtech not interpreting it as italics.

You only have to worry about zero-based indeces when you access array items (i.e. the first item is var[0], second item is var[1], last item is var[9]), the number you put in the declaration is the number of items. So:

int i, var[10];

for(i=0; i<10; i++)
var[ i ] = i+1;

I think he knew that, that's why he declared it of length 11. He doesn't like standards 😛. My your method (the more accepted, and IMO the better), the array would be declared of length 10 (0-9 indexes) not 11 (0-10 indexes).
 
The reason I'm asking for variable variables is because I want to make a never-ending loop which creates a different variable each time through (can't write past the end of an array...) Something like this:

int i;
for(i = 0; i <= 10; i++)
{
int variablei = i;
}

What I'm looking to do is make a program that creates variables on the freestore and create a memory leak in a few seconds... (just to see what it would be like...)
 
if all you want to do is make a memory leak, then

for( ; ; ) //put in your own limits
malloc(10000); //change number of bytes to allocate to more or less, if you like
 
this will make a nice one.

void leako()
{
std::vector<int*> vari;
for (int i=1; i<=10; i++)
{
vari.push_back(new int);
}
}
 
You cannot create variables on the fly in C/C++.

Well not in the sense you want to.
Sciencewhiz gave the easiest way to create a memory leak. And probably the most common, create memory and don't delete it.
Repeat, It doesn't get any simpiler than that. You don't really need the variables, you need the memory. Thats what malloc or new is for.

 
Back
Top