Light User Data value changed

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Light User Data value changed

aryajur
Hello,
           I am trying to use Light User Data to point to a 64 bit integer in my C module. I pass the address of the 64 bit integer as light user data back to lua. When Lua calls another function passing it the light user data then the value at the pointer is different. I have attached the code for the C module. And the run output from Lua:

-----C Module------
#include <lua.h>
#include <lauxlib.h>
#include <stdint.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <compat-5.3.h>

void * cp;

static int
makeNew(lua_State *L) {
int64_t n;
    lua_Number d = lua_tonumber(L,1);
    n = (int64_t)d;
    printf("The number in function new is %lli\n",n);
void * p = (void *)&n;
cp = p;
printf("Pointer to the number is 0x%p\n",p);
printf("Value at the pointer is %lli\n",*(int64_t *)p);
printf("Value at creation pointer is %lli\n",*(int64_t *)cp);
lua_pushlightuserdata(L,p);
return 1;
}

static int
tostring(lua_State *L) {
void * p = lua_touserdata(L,1);
printf("Pointer returned is 0x%p\n",p);
int64_t n = (*(int64_t *)p);
printf("Number at the pointer is %lli\n",n);
printf("Creation pointer is 0x%p\n",cp);
printf("Number at creation pointer is %lli\n",*(int64_t *)cp);
return 0;
}

int
luaopen_test(lua_State *L) {
lua_newtable(L);
lua_pushcfunction(L, makeNew);
lua_setfield(L, -2, "new");
lua_pushcfunction(L, tostring);
lua_setfield(L, -2, "tostring");
return 1;
}

--- Lua 5.1.5 Output -----
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> test = require("test")
> a = test.new(2^40)
The number in function new is 1099511627776
Pointer to the number is 0x000000000022F598
Value at the pointer is 1099511627776
Value at creation pointer is 1099511627776
> test.tostring(a)
Pointer returned is 0x000000000022F598
Number at the pointer is 2291096
Creation pointer is 0x000000000022F598
Number at creation pointer is 2291096
>


Please direct me to what I am doing wrong here.

Thanks,
Milind
Reply | Threaded
Open this post in threaded view
|

Re: Light User Data value changed

Jonathan Goble
On Fri, Apr 19, 2019 at 3:35 PM Milind Gupta <[hidden email]> wrote:
> test.tostring(a)
Pointer returned is 0x000000000022F598
Number at the pointer is 2291096
Creation pointer is 0x000000000022F598
Number at creation pointer is 2291096

There's a clue here: the number you're getting in this output (2291096 base-10) is equal to the pointer (22F598 base-16) according to my programmer's calculator.

Unfortunately, my C skills are too rusty to debug beyond that.
Reply | Threaded
Open this post in threaded view
|

Re: Light User Data value changed

Albert Chan
In reply to this post by aryajur

On Apr 19, 2019, at 3:35 PM, Milind Gupta <[hidden email]> wrote:

> Hello,
>            I am trying to use Light User Data to point to a 64 bit integer in my C module. I pass the address of the 64 bit integer as light user data back to lua. When Lua calls another function passing it the light user data then the value at the pointer is different. I have attached the code for the C module. And the run output from Lua:

Your 64-bit integer, n,  is a local variable.
Once out of scope, is undefined.


Reply | Threaded
Open this post in threaded view
|

Re: Light User Data value changed

aryajur

Your 64-bit integer, n,  is a local variable.
Once out of scope, is undefined.


Thank you! 
Reply | Threaded
Open this post in threaded view
|

Re: Light User Data value changed

Tim Hill
In reply to this post by Albert Chan


--Tim

> On Apr 19, 2019, at 2:57 PM, Albert Chan <[hidden email]> wrote:
>
>
>> On Apr 19, 2019, at 3:35 PM, Milind Gupta <[hidden email]> wrote:
>>
>> Hello,
>>           I am trying to use Light User Data to point to a 64 bit integer in my C module. I pass the address of the 64 bit integer as light user data back to lua. When Lua calls another function passing it the light user data then the value at the pointer is different. I have attached the code for the C module. And the run output from Lua:
>
> Your 64-bit integer, n,  is a local variable.
> Once out of scope, is undefined.
>
>