icy icy
icy icy

Reputation: 33

Why is this program using negative ints specifically?

I'm reading over the C API Lua documentation, and I notice in this code:

lua_pushnil(L);  /* First key */
while (lua_next(L, t) != 0) {
  /* Uses 'key' (at index -2) and 'value' (at index -1) */
  printf("%s - %s\n",
         lua_typename(L, lua_type(L, -2)),
         lua_typename(L, lua_type(L, -1)));
  /* Removes 'value'; keeps 'key' for next iteration */
  lua_pop(L, 1);
}

It looks normal, apart from the lines where it uses -2 and -1 for the stack indexes.

I tried changing them to positives, and it didn't seem to have any affect on the program. Why would they specifically use negatives? Why doesn't this cause a compiler error? I'm sure you can't use unsigned ints with negative numbers.

I don’t know if it makes any changes, but I'm using C++ instead of C, so maybe that has something to do with it?

Upvotes: 0

Views: 273

Answers (2)

Shivam
Shivam

Reputation: 1

In Lua's C API, the stack indices are allowed to be negative integers, and they are interpreted as indices from the top of the stack. The index -1 refers to the top of the stack, -2 refers to the second element from the top, and so on. This convention is convenient for working with the stack in a LIFO (Last In, First Out) manner, which is typical for stack-based programming.

In the code you provided:

    printf("%s - %s\n",
       lua_typename(L, lua_type(L, -2)),
       lua_typename(L, lua_type(L, -1)));

Here, -2 refers to the key and -1 refers to the value on the Lua stack. The negative indices are used to access elements relative to the top of the stack, making it easy to work with the key-value pairs returned by lua_next.

The reason you didn't see any difference when changing them to positives is because Lua allows you to access the stack from the top using negative indices or from the bottom using positive indices. So, if n is a positive index, it refers to the element at index n from the bottom of the stack.

Regarding the use of unsigned integers, Lua's API doesn't use unsigned integers for stack indices. The stack indices are of type int, and they can be both positive and negative. If you are using C++, which is a more strongly typed language than C, you might see a warning about converting signed and unsigned integers, but it doesn't result in a compiler error.

In summary, the use of negative indices in Lua's C API is a convention for accessing elements from the top of the stack, and it's not related to the use of unsigned integers. The code is designed this way for simplicity and consistency in working with Lua's stack

Upvotes: -2

Markus Safar
Markus Safar

Reputation: 6580

According to the documentation:

For convenience, most query operations in the API do not follow a strict stack discipline. Instead, they can refer to any element in the stack by using an index: A positive index represents an absolute stack position, starting at 1 as the bottom of the stack; a negative index represents an offset relative to the top of the stack. More specifically, if the stack has n elements, then index 1 represents the first element (that is, the element that was pushed onto the stack first) and index n represents the last element; index -1 also represents the last element (that is, the element at the top) and index -n represents the first element.

Upvotes: 0

Related Questions