In lua, objects are tables, so the question is the same as “are tables stored as pointers”.
If you know about other languages, like c++ or c#, are objects there stored as pointers too?
In lua, objects are tables, so the question is the same as “are tables stored as pointers”.
If you know about other languages, like c++ or c#, are objects there stored as pointers too?
Not sure you mean “stored” but maybe passed. In Lua, Objects are passed by ref (the same as tables, so when you pass them you pass the exact same object so all changes will be done to the original one). Only primitives are passed by value (a “copy” of the original, not really but conceptually).
Stored in memory, I guess it’s the same as when it’s passed.
@rotbotrotbot was saying passed, because “stored as a pointer” doesn’t make too much sense.
A pointer is like a house address written on a envelope. An object is like the house.
Your question is like “where are houses? On envelopes?”.
To which the answer is “no?” with a bit of confusion
If you called PaintRed(house)
in lua, then yeah, maybe the c side is translated as paint_red(ptrAddressOfHouse)
. But just because it’s passed by reference (pointer) doesn’t change where the house physically lives.
Objects (houses?) are stored in RAM. More or less.
Pointers do have addresses themselves, since they are seperate variables pointing to another variables.
You can have a a variable
int Var = 6
And a pointer
int* p = &Var
Pointer p stores the address of Var, lets say it’s 94, Pointer p stores 94, but it has an address too, let’s say it’s 98.
If I read the memory address of p, which is 98, I will get the memory address of Var stored in p, which is 96. Pointers have addresses, they can’t just exist in some other way.
For example, in c++, arrays are stored as pointers.
So if you do something like
int arrai[5] = {11,12,13,14,15};
and print arrai onto console, you would get the address of the first element (11) in the array. So arrai is actually a pointer, it doesn’t hold the value of any of the elements, it holds an address to the first element.
It’s also impossible to pass it by value, since it’s not a value itself, its elements are, so you could pass them by value, one by one, but arrai is not.
The address of the next element of that array would be arrai + sizeof(int), then the next one would be arrai + sizeof(int) * 2, and so on.
So arrai doesn’t actually store the values.
Now I want to know, when I make an object (table), the variable that this object (table) is assigned to, is it a pointer to somewhere else, or does it hold all the values in one variable, in some weird way.
If you know how objects are stored in lua or any other languages, please tell me. I’m 100% sure that objects in c++ are not arrays, since arrays can’t have string indexes, so knowing how arrays are stored, doesn’t tell me how objects are stored.
In lua, objects are tables, so if I know how tables are stored, it would tell me how objects are stored.
I get all that I just meant that nothing is “stored as a pointer”. The array is physically stored as bytes in RAM. The pointer is just the location. We’re saying the same thing though
See page 6 of http://www.lua.org/doc/jucs05.pdf
tl;dr: there’s an array part and a hash table part.
A pointer is a variable that stores an address, the array is stored as values, but the variable that you assign that array to, is a pointer to the first value there.