Forum

> > CS2D > Scripts > How to use address?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to use address?

20 replies
Page
To the start Previous 1 2 Next To the start

old How to use address?

mozilla1
User Off Offline

Quote
I wonder if there's a way to work around the address of tables and functions, you know... when you use "tostring" in a table or function value and it returns something like that: "table:045D0066"

Is it possible? If so, I beg your help.
Sorry for my poor english.

old Re: How to use address?

Infinite Rain
Reviewer Off Offline

Quote
Wait what do you want exactly? You want to get table turned into a string, like to save the output string to the file?

old Re: How to use address?

Nekomata
User Off Offline

Quote
He wants to know if it's possible to print the table/variable's address allocated in the memory.

Something like this in c++

int address = 20;
cout << "This is the address of that variable: " << &address;

old Re: How to use address?

mozilla1
User Off Offline

Quote
no, @user Infinite Rain: . I don't need table serialization... I need something like this output:


1
2
3
4
5
6
7
8
9
10
mytable={"foo","bar","lua"}

tostring(mytable) -- returns "table:12345"

table_toaddress(mytable) -- returns 12345
address_totable(12345) -- returns mytable

mytable = nil

address_totable(12345) -- returns nil

old Re: How to use address?

Hostagessy
BANNED Off Offline

Quote
That's what you have written is NOT possible, because every time you start a server, all tables will have a different address.

Only this is possible to do:
table_toaddress(mytable)

old Re: How to use address?

mozilla1
User Off Offline

Quote
@user Hostagessy: I'm aware of this, and don't think this would be an issue, I only need the addresses for the present-time being, like, for navigation. So, the randomness thing for the addresses woudn't affect my objectives, if this woudn't be for saving things.

But thanks for helping anyway.

old Re: How to use address?

VADemon
User Off Offline

Quote
I don't think anybody understands what you want to do. You said that you don't need serialization, but that's what you want to do with the table (table_toaddress(mytable)) and finally you just do the opposite (address_totable(12345)).
Do you want to encrypt/archive stuff in any way? As I understood, you only care about the contents of the target table.

old Re: How to use address?

Infinite Rain
Reviewer Off Offline

Quote
He basically wants to get a table knowing its address, that's what I understood.

Ok for example I have table of elements called "myTable". And I get its address via tostring(myTable). Output would be something like: "0x4801c890" and if you know its address you can use it like that:
1
print(0x4801c890[1])

Basically you can use its address as if it was a name of the table/function. In our case codes
1
print(0x4801c890[1])
and
1
print(myTable[1])
will have the same effect.

But using its address instead of the name is pointless because they change every run, that means that the only way to know it would be storing its address in a string variable and execute it via the loadstring(s) command, but again, why the fuck would you ever want do that.
edited 9×, last 19.11.14 12:04:12 pm

old Re: How to use address?

DC
Admin Off Offline

Quote
I also don't get why you want to use the address at all. I mean: Why the address? What do you want to do with it?

If you want to save the reference to a table you can simply do:
1
2
3
4
5
6
mytable = {2, 4, 6}  -- create a table

tmp = mytable -- create a variable which points to that table. This WON'T copy the table!
tmp = {1, 3, 5} -- change mytable using tmp

'mytable will now contain {1, 3, 5} (and tmp as well as it is the same table in the memory)

In the sample above "tmp" behaves like the address/pointer/reference of "mytable".

There is no need to access the address of tables in Lua manually. I think that you're doing something wrong if you want to do that.

old Re: How to use address?

useigor
User Off Offline

Quote
Wait. As far as i know tmp = mytable - is just copying. Now we have 2 different tables. And mytable won't change if you change tmp.

1
2
print(mytable,tmp)
for k,v in pairs(mytable) do print(k,v) end

Well, maybe i didn't understand something.

old Re: How to use address?

VADemon
User Off Offline

Quote
user useigor if you do that, you will have two variables pointing to the same source (table). You can only create a full copy by doing pairs() on the source table.
1
2
3
4
5
source = {4,7,2}
alternative = {}
for k, v in pairs(source) do
alternative[k] = v
end

old Re: How to use address?

useigor
User Off Offline

Quote
I do and i have two different tables (at least changing of second table doesn't affect on first and also they have different addresses). Well, maybe second table points to first until the change.

user DC has written
1
2
tmp = mytable -- create a variable which points to that table. This WON'T copy the table!
tmp = {1, 3, 5} -- change mytable using tmp
It doesn't change mytable. I tested on http://www.lua.org/demo.html with the prints above. I didn't test in CS2D.

old Re: How to use address?

DC
Admin Off Offline

Quote
Ooops. Okay, my bad then... I expected it to work that way... You are right. What I wrote is obviously wrong.

old Re: How to use address?

VADemon
User Off Offline

Quote
user useigor and user DC
The tmp table points to mytable as long as you DON'T REDEFINE IT:
1
tmp = {1,10,20}
Code examples >

I gotta keep that in mind too

old Re: How to use address?

MikuAuahDark
User Off Offline

Quote
Basically, what do you(user mozilla1) want to do is not possible in normal lua(unedited source code). The address is just for debugging puprose. It also does not make sense, why you want to use address? why you not use variable instead(like user DC, user useigor, and user VADemon) said?
Please note that Lua does have garbage collection which destroys the object when it doesn't needed. Example
1
2
3
temp={1,2,3,4,5}
print(temp)	-- table: 0xFFFFFFFF
temp=nil	-- now table with address FFFFFFFF is destoryed by lua
If you're advanced enough, printing tables(and another object in lua) does work like this for example
1
2
3
void print_object(lua_State* L,int n) {
	printf("%s: %X",lua_typename(L,n),lua_topointer(L,n));
}
as you can see it does calling lua_topointer to get it's address.
lua_topointer has written
Converts the value at the given acceptable index to a generic C pointer (void*). The value can be a userdata, a table, a thread, or a function; otherwise, lua_topointer returns NULL. Different objects will give different pointers. There is no way to convert the pointer back to its original value.

Typically this function is used only for debug information.


@user VADemon: It also apply for coroutine(thread), userdata, and function.

old Re: How to use address?

mozilla1
User Off Offline

Quote
It is too late, I already did it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function table.lookup(address)
	local address_found={}
	function lookup(n,t,a)
		if value_found then return end
		a = a or {}
		
		if type(t) =="table" then
			if a[t] then
				return
			else
				if tostring(t)==address then
					address_found=t
					return 
				else
					a[t] = true
					for k,v in pairs(t) do
						lookup(k,v,a)
					end
				end	
			end
		end
	end	
	lookup("_G",_G)
	
	return address_found
end

old Re: How to use address?

MikuAuahDark
User Off Offline

Quote
@user mozilla1: that's apply only for tables that stored in global variable. What about tables that placed in regstry variable or in another table? you won't get the table.
To the start Previous 1 2 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview