Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:28:16+00:00 2026-06-09T09:28:16+00:00

I’m trying Lua and want to know how lua_State working code and result: state.c

  • 0

I’m trying Lua and want to know how lua_State working
code and result:

state.c

#include <stdio.h>
#include "lua/src/lua.h"
#include "lua/src/lauxlib.h"
static void stackDump(lua_State *L){
    int i;
    int top = lua_gettop(L);
    for(i = 1; i<= top; i++) {
        int t = lua_type(L, i);
        switch(t){
        case LUA_TSTRING:
            printf("'%s'", lua_tostring(L, i));
            break;
        case LUA_TBOOLEAN:
            printf(lua_toboolean(L, i) ?"true":"false");
            break;
        case LUA_TNUMBER:
            printf("%g", lua_tonumber(L, i));
            break;
        default:
            printf("%s", lua_typename(L, t));
            break;
        }
        printf(" ");
    }
    printf("\n");
}

static int divide(struct lua_State *L){

    double a = lua_tonumber(L, 1);
    double b = lua_tonumber(L, 2);
    printf("%p\n", L);

    stackDump(L);

    int quot = (int)a / (int)b;
    int rem = (int)a % (int)b;

    lua_pushnumber(L, quot);
    lua_pushnumber(L, rem);

    stackDump(L);
    printf("---end div---\n");

    return 2;
}

int main(void){
    struct lua_State *L = lua_open();
    lua_pushboolean(L, 1);
    lua_pushnumber(L, 10);
    lua_pushnil(L);
    lua_pushstring(L, "hello");

    printf("%p\n", L);

    stackDump(L);

    lua_register(L, "div", divide);
    luaL_dofile(L, "div.lua");

    stackDump(L);
    lua_close(L);
    return 0;
}

div.lua
local c = div(20, 10)

0x100c009e0
true 10 nil ‘hello’
—start div—
0x100c009e0
20 10
20 10 2 0
—end div—
true 10 nil ‘hello’

I see lua_State in divide is the same with the main one, but they have different data in stack, How this be done ?

I know the best way to understand this is to read source code of Lua , maybe you can tell me where to find the right place.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:28:17+00:00Added an answer on June 9, 2026 at 9:28 am

    It gets different data the same way anything gets different data: code changes the data inside of the object.

    struct Object
    {
      int val;
    };
    
    void more_stuff(Object *the_data)
    {
      //the_data->val has 5 in it now.
    }
    
    void do_stuff(Object *the_data)
    {
      int old_val = the_data->val;
      the_data->val = 5;
      more_stuff(the_data);
      the_data->val = old_val;
    }
    
    int main()
    {
      Object my_data;
      my_data.val = 1;
    
      //my_data.val has 1.
      do_stuff(&my_data);
      //my_data.val still has 1.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.