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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:25:21+00:00 2026-05-16T20:25:21+00:00

I’m embedding Lua in a C++ application using Lua5.1 and I’m having an odd

  • 0

I’m embedding Lua in a C++ application using Lua5.1 and I’m having an odd issue with luaL_newstate().

This works:

lua_State *L = NULL;
int main()
{
   L = luaL_newstate();
   return 0;
}

I recently restructured my code and chose to create an init function like this:

lua_State *L = NULL;
void init_lua(lua_State *L)
{
   L = luaL_newstate();
}
int main()
{
   init_lua(L);
   return 0;
}

That doesn’t work. For some reason, luaL_newstate() always returns NULL in that situation. But, to add to the confusion, this does work:

lua_State *L = NULL;
void init_lua(lua_State **L)
{
   *L = luaL_newstate();
}
int main()
{
   init_lua(&L);
   return 0;
}

Functionally, I don’t see a difference between the second and third examples and yet the second segfaults as soon as I attempt a lua call using L and the third works just fine. What is happening here?

  • 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-16T20:25:21+00:00Added an answer on May 16, 2026 at 8:25 pm

    In the second example, this function:

    void init_lua(lua_State *L)
    {
      L = luaL_newstate();
    }
    

    You are setting L to the return of luaL_newstate(). L is a pointer to a lua_state. However, you are only changing the parameter version of L.

    In you third example:

    void init_lua(lua_State **L)
    {
      *L = luaL_nwstate();
    }
    

    You are setting the value pointed to by L (which is your globally defined L) to the return of luaL_newstate(). So you are changing the actual variable passed to the function.

    In summary: when you pass a VALUE in the second example, the function cannot change the original variable. But when you pass a POINTER to your variable in your third, the function can change the value of that variable using the address you passed to it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.