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 6648173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:37:26+00:00 2026-05-26T00:37:26+00:00

I would be really appreciative if someone explain me how lua-C stack works when

  • 0

I would be really appreciative if someone explain me how lua-C stack works when lua function called from C returns two tables or table which has nested table inside

when I am trying to do it it seems to look fine but only at first glimpse:

if ( lua_istable(L, -1) )
        printf("its OK\n");

    if ( lua_istable(L, -2) )
        printf("its OK\n");

    lua_pushnil(L);

        while ( lua_next(L, -2) )
        {
            if(lua_isnumber(L, -1)) 
            {
                int i = (int)lua_tonumber(L, -1);
                const char *key = lua_tostring(L, -2); 
                printf("%d %s \n", i, key); 
            }
            lua_pop(L, 1);
        }
        lua_pop(L, 1);

In this case I got two messages that first table is on level -1, on the second one is on level -2, but afterthis code, when I am trying to get the next table my program crashes
when I check stack on table existence

for ( int i = -2; ; --i)
            if ( lua_istable(L, i) )
                printf("its %d OK\n", i);

I got following result:

its -233 OK
its -645 OK
its -1245 OK
its -1549 OK
its -2807 OK
its -2815 OK
its -2816 OK

can somebody help me out with this ?

  • 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-05-26T00:37:27+00:00Added an answer on May 26, 2026 at 12:37 am

    Note that when lua_next returns 0 is has popped the key, and pushed nothing, so at the end of the while loop you have your two tables on the stack.

    The lua_pop after your while loop is popping the top table off the stack.

    The subsequent for loop is starting at index -2 which is past the table, and could contain anything. Furthermore, the for loop will never terminate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would really appreciate it if some of you could help optimize my tables,
I would really appreciate if someone could give me advice,or point me to tutorial,
I would really appreciate any suggestions, no matter how simple or complex, to help
I would really appreciate some feedback on what I am trying to achieve: The
I would really appreciate if you could shed light on this problem. I have
I would really appreciate if you would take a look at this piece of
Ok I give up and would really appreciate it if you guys could cast
I would really like to see a proportional font IDE, even if I have
I would really love to push for TDD development inside the shop I'm working
I would really like to annotate a method with a reference to a single

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.