首页 > LUA > lua-栈的概念
2016三月21

lua-栈的概念

[隐藏]

1.栈布局

熟练使用Lua,最重要的就是要时刻知道什么时候栈里面的数据是什么顺序,都是什么。如果你能熟练知道这些,实际你已经是Lua运用的高手了。当你初始化一个栈的时候,它的栈底是1,而栈顶相对位置是-1,说形象一些,你可以把栈想象成一个环,有一个指针标记当前位置,如果-1,就是当前栈顶,如果是-2就是当前栈顶前面一个参数的位置。以此类推。当然,你也可以正序去取,这里要注意,对于Lua的很多API,下标是从1开始的。这个和C++有些不同。而且,在栈的下标中,正数表示绝对栈底的下标,负数表示相对栈顶的相对地址,这个一定要有清晰的概念,否则很容易看晕了。

1.png

注:其实都是从栈底到栈顶,索引数值递增

示例:

	lua_context L;
	lua_pushnumber(L, 11);
	lua_pushnumber(L, 12);
	int nIn = lua_gettop(L);//索引是从1开始的,所以返回的结果就是栈中元素的个数,返回0表示栈是个空栈
	assert(nIn == 2);

	int nData = lua_tonumber(L, -1);//< 读取栈顶第一个元素 
	assert(nData == 12);
	nData = lua_tonumber(L, -2);//< 读取栈顶第二个元素 
	assert(nData == 11);				
	nData = lua_tonumber(L, 1);//< 读取栈底第一个元素 
	assert(nData == 12);
	nData = lua_tonumber(L, 1);//< 读取栈底第二个元素 
	assert(nData == 11);

栈中存储如下:

1.png

注意函数调用时是会把函数名先压栈的,无论这函数存不存在,都会压栈

lua_pcall()函数在调用成功之后,会自动的清空栈,然后把结果放入栈中

function fun1(x,y)
return x,y
end
	 luaL_openlibs(L);// 它内部调用了lua_pop(L, 1),所以不影响堆栈
	 int iErr = luaL_dofile(L,FILEPREX("1.lua"));
	 assert(iErr == 0);
	
	 //验证Lua函数是否在你当前加载的Lua文件中,并把指针指向这个函数位置
	 lua_getglobal(L, "fun1");//大小写敏感
	 nIn = lua_gettop(L);	// 调用的函数名称占据了一个堆栈的位置
	 assert(nIn == 1);
	 
	 lua_pushnumber(L, 11);// 压栈(顺序:从左到右的参数):第一个参数
	 lua_pushnumber(L, 12);// 压栈(顺序:从左到右的参数):第二个参数
	 iErr = lua_pcall(L,2,2,0); // 执行这个函数,前2是输入参数的个数,后2是返回值的个数
	 assert(iErr == 0);
	 nIn = lua_gettop(L);
	 assert(nIn == 2);// 返回几个值,这就是几
	 int nOut1 = lua_tonumber(L, -1);
	 assert(nOut1 == 11);
	 int nOut2 = lua_tonumber(L, -2);
	 assert(nOut2 == 12);

  

2.栈清除

lua_settop(L,0);//栈顶指针目前在当前位置的0的位置上(1以下)
lua_tonumber()lua_tostring()lua_touserdata()一定要将数据完全取出后保存到你的别的变量中去,否则会因为清栈操作,导致你的程序异常

文章作者:hgy413
本文地址:https://hgy413.com/3109.html
版权所有 © 转载时必须以链接形式注明作者和原始出处!

19 Responses to “lua-栈的概念”

  1. #1 minecraft 回复 | 引用 Post:2018-10-04 21:35

    Hello there, There’s no doubt that your blog could be having
    internet browser compatibility issues. When I look
    at your web site in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues.

    I simply wanted to provide you with a quick heads up!
    Besides that, excellent website!

  2. #2 minecraft 回复 | 引用 Post:2018-10-05 08:57

    Hi to every single one, it’s really a nice for me to visit this website, it includes valuable Information.

  3. #3 minecraft 回复 | 引用 Post:2018-10-06 06:17

    Greetings from Colorado! I’m bored to tears at work so I decided
    to check out your website on my iphone during lunch
    break. I enjoy the knowledge you present here and can’t wait to take a look when I get home.
    I’m amazed at how fast your blog loaded on my mobile ..
    I’m not even using WIFI, just 3G .. Anyways, wonderful site!

  4. #4 minecraft 回复 | 引用 Post:2018-10-07 19:50

    I really like your blog.. very nice colors & theme.
    Did you create this website yourself or did you hire someone to do it for you?
    Plz reply as I’m looking to create my own blog and would
    like to find out where u got this from. thanks a lot

  5. Awesome website you have here but I was wondering if
    you knew of any user discussion forums that cover the same topics discussed here?
    I’d really love to be a part of community
    where I can get suggestions from other experienced people that share the same interest.

    If you have any recommendations, please let me know. Kudos!

  6. I always spent my half an hour to read this webpage’s articles or
    reviews everyday along with a mug of coffee.

  7. You really make it seem so easy with your presentation but I find
    this matter to be actually something that I think I would never understand.
    It seems too complicated and extremely broad
    for me. I am looking forward for your next post,
    I’ll try to get the hang of it!

  8. #8 Benefits of Coconut Oil 回复 | 引用 Post:2018-10-21 12:42

    What i do not understood is if truth be told how you’re no longer really a lot more neatly-favored than you may be now.
    You’re so intelligent. You realize therefore considerably on the subject of this topic, produced me in my view imagine it
    from a lot of various angles. Its like men and women don’t seem to be involved unless it is one
    thing to accomplish with Lady gaga! Your personal stuffs outstanding.

    Always take care of it up!

  9. #9 quest bars cheap 回复 | 引用 Post:2018-11-03 04:45

    It’s truly very difficult in this busy life to listen news on Television, therefore I just use web for that reason, and obtain the most
    up-to-date news.

  10. #10 quest bars cheap 回复 | 引用 Post:2018-11-04 14:04

    Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap
    Hi outstanding blog! Does running a blog like this take a large amount of work?

    I have virtually no expertise in programming but I had been hoping to start
    my own blog in the near future. Anyways, should
    you have any suggestions or tips for new blog owners please share.
    I understand this is off subject but I simply needed to ask.
    Thanks! Quest bars cheap fitnesstipsnew1 quest bars cheap 516999410492780544 quest bars cheap

  11. #11 quest bars 回复 | 引用 Post:2018-11-06 05:50

    Hey there! This post couldn’t be written any better! Reading through this
    post reminds me of my old room mate! He always kept talking about this.
    I will forward this page to him. Pretty sure he will
    have a good read. Thanks for sharing!

  12. #12 Sling TV 回复 | 引用 Post:2018-11-10 23:43

    I seriously love your blog.. Very nice colors & theme. Did you
    make this amazing site yourself? Please reply back as I’m hoping to create
    my own blog and would love to know where you got this from or what the theme is called.

    Appreciate it!

  13. I do agree with all the concepts you’ve presented in your post.

    They’re really convincing and can certainly work. Nonetheless, the posts are
    very short for starters. May you please lengthen them a bit
    from subsequent time? Thanks for the post.

  14. Hello there! This is my first comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your posts.

    Can you recommend any other blogs/websites/forums
    that go over the same subjects? Thank you!

  15. Good blog you’ve got here.. It’s difficult
    to find quality writing like yours these days.
    I seriously appreciate individuals like you! Take care!!

  16. #16 spoon sports lanyard 回复 | 引用 Post:2020-05-14 12:02

    First off I want to say awesome blog! I
    had a quick question which I’d like to ask if you do not mind.

    I was interested to know how you center yourself and clear your thoughts prior to writing.
    I have had a tough time clearing my mind in getting my ideas
    out there. I do enjoy writing however it just seems like the first 10 to 15 minutes
    are usually lost simply just trying to figure out how to begin. Any ideas or
    hints? Kudos!

  17. #17 g from 回复 | 引用 Post:2020-06-10 18:07

    Magnificent goods from you, man. I have understand your stuff previous to
    and you’re just too excellent. I actually like what you’ve
    acquired here, really like what you’re saying and the way in which you say it.
    You make it entertaining and you still take care of to
    keep it wise. I can not wait to read much more from you.
    This is really a great site.

  18. #18 g would 回复 | 引用 Post:2020-06-11 01:56

    Aw, this was a very good post. Taking a few minutes and actual effort to generate a really
    good article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.

  19. #19 tinyurl.com 回复 | 引用 Post:2020-06-14 11:56

    For newest information you have to pay a visit
    world-wide-web and on internet I found this site as a most
    excellent site for latest updates.

发表评论