Creating a variable while running

Hi, I want to know how I can create a variable while my program is running, and I want to name it while the program is running. Like I want to give it a type, name, and value while the program is running. Thanks in advance
It doesn't make any sense to create a variable name at runtime, because the sole purpose of a variable name is to reference the variable at compile time (ie: during runtime, variable names have no use).

Dynamic typing (creating different types) is possible but can be a little tricky depending on exactly what it is you're trying to do.

If you're talking basic types, like int, float, or char... it's kind of a pain. The dirty way is to use a union, with an enum to indicate the type -- but a safer approach is to use polymorphism (ie: put everything in a class, derive different classes for different types, or fit in templates somehow)

But I question whether or not this is really what you are looking for. What exactly are you trying to do? Like what do you need to do this for?
I just want to know if possible. Like instead of doing this:

int main()
{
int A;
A = 1;
return 0;
}

How could I do it while the program is running.
Actually create an integer variable named A with a value of 1. How would I do this while running the program.
You can't.
Are you sure because I think its possible.
Like mabey if there was a function that did it.
Mabey something like:

newVar( Type, Name, Value);

So while the program was running I could create a new variable. Because I don't see why I can't free some memory while the program was running and make a new variable. I do know memory is freed by your compiler depending on variables you initialize in the source, but I am sure there is a way.
After your program gets compiled, you won't have any symbol any more, everything becomes just data.
Things like these are only possible for scripting languages
Last edited on
Everything becomes data as in instructions for your computer. So why can't some of these instructions freespace for and make a new variable. Can you explain it a little better. I am somewhat new and still think its possible.
Let's assume, for argument's sake, that it is possible. Say you get the variable name from the user:

1
2
3
4
5
string varname;
cout << "What do you want the variable name to be?  ";
cin >> varname;

newVar( int, varname, Value );


Then what? How do you access this variable in your program? It has no name at compile time, so there's no way to reference it in your code (at least not without giving it another name -- which defeats the point).

This makes no sense.

EDIT:

Dynamically creating variables is certainly possible. see Return 0's links.
You can even make the type of variable you create dynamic. The name, however, has to be something fixed in your code.
Last edited on
Same as Bazzy here: You can't.

Hypothetically speaking: Say you can. How do you decide the variable's name? User input? Let's say user input. So, where are you storing the name of the new variable that is provided by user input? Ok, a string variable. Call it szVarName. Let's say the user decides the type. Where do you save that type selection? A variable? Of what type? Oh oh. First problem. This one can actually be resolved in a number of ways. I can imagine an enumeration and the user states the type by selecting from the enumeration. Now the value. To store the value you need the variable, because, after all, the variable's purpose is to hold the value:

newVar(eSelectedType, szVarName, NULL);

That function call returns... what? You can continue with this approach, but you'll end up with a totally different thing. So, this approach doesn't seem feasible, at least in the sense that you want it.

But let's analyze what a variable is: A variable is a human-readable name to reference a number of allocated bytes. That is all. Do you want to create a variable in runtime? Following the process of substitution, I can rephrase to: Do you want to create human-readable name to refrence a number of allocated bytes? Allocated bytes mean reserved memory for use, or basically, referring to the active verb "to allocate", creating a variable means allocating memory. Sure you can allocate memory. Use the new() operator.
When you compile a program, all the information about your variable names are lost. You would only get memory addresses.

If you tell your computer 'set x to 123' it wouldn't know what to do. If you instead tell it 'set the byte at memory address 0x123456 to 123' he would understand what you are saying. During runtime you are able to allocate new memory but you can't refer to the new object with a name, you can only use its memory address

When declaring a variable eg:
int x; the keyword 'auto' is omitted but it can explicited, it would be as writing auto int x;
which is opposed to static int x; and to int *x = new int;

'static' means that 'x' would be allocated only once and it will last until the end of your program.
'auto' means that 'x' allocation/destruction would be handled automatically based on 'x' scope. You cannot allocate automatic memory during runtime.
'new' means that a new int would be allocated when you want it to happen but that object wouldn't have any name. 'x' ( auto int * ) will contain only its memory address.


ok thank you
good explanation bazzy
Topic archived. No new replies allowed.