Q:

static struct array prints garbage after loading lua file

I have a static struct array of which is filled by data parsed from a lua file.
I thought I was doing every right, but as soon as I went to load another lua file, the static array started printing garbage. I'm not exactly sure what's going on behind the scenes with loading the lua file, but I haven't been able to reproduce it any other way.
Here is a my code and the lua file I am loading.
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define MAX_FILES 5
#define MAX_BOXES 25

struct Box
{
  const char *name;
  int count;
  const char *files[MAX_FILES];
};

static struct Box boxes[MAX_BOXES];
static int box_index = 0;

void script_bail (lua_State * S, const char *fmt, ...)
{
  va_list argp;
  va_start(argp, fmt);
    vfprintf(stderr, fmt, argp);
  va_end(argp);
  lua_close(S);
  exit(EXIT_FAILURE);
}

int box_create (const char* name, int count, const char **files)
{
  if (box_index + 1 >= MAX_BOXES)
    return 1;

  boxes[box_index].name = name;
  boxes[box_index].count = count;
  memcpy(boxes[box_index].files, files, count * sizeof(char*));

  box_index++;

  return 0;
}

void load_boxes (lua_State* S)
{
  const char * name;
  const char * files[MAX_FILES];

  int element_index = 1;
  int file_index = 0;

  lua_getglobal(S, "boxes");

  if (!lua_istable(S, -1))
    script_bail (S, "'%s' is not a table\n", "boxes");

  /* each element has no key */
  lua_pushnil(S);

  /* iterate through this table of tables */
  while (lua_next(S, -2)) {

    if (!lua_istable(S, -1))
      script_bail (S,
          "Element %d of table `classes' is not a table!\n",
          element_index);

    /* find variable by name `name' and make sure it's a string */
    lua_pushstring(S, "name");
    lua_gettable(S, -2);

    if (!lua_isstring(S, -1))
      script_bail (S, "invalid field in table for key: %s", "name");

    name = lua_tostring(S, -1);

    lua_pop(S, 1);

    /* get the table where that's named `components' */
    lua_pushstring(S, "files");
    lua_gettable(S, -2);

    if (!lua_istable(S, -1))
      script_bail (S,
          "files of element %d is not a table!\n",
          element_index);

    /* each component has no key */
    lua_pushnil(S);

    /* go through each component */
    while (lua_next(S, -2)) {

      if (!lua_isstring(S, -1))
        script_bail (S,
            "Element %d of files of box %d is not a string!\n",
            file_index, element_index);

      /* add the component file to our index and increment the index */
      files[file_index++] = lua_tostring(S, -1);

      /* pop current to move to next */
      lua_pop(S, 1);
    }

    if (box_create(name, file_index, files) != 0)
      script_bail(S, "boxes exceeds limit of %d\n", MAX_BOXES);

    /* pop the files table */
    /* pop the current table to make room for the next */
    lua_pop(S, 2);

    element_index++;
    file_index = 0;
  }

  lua_pop(S, 1);
}

lua_State * load_lua_file (const char *filename)
{
  lua_State * S = luaL_newstate();
  luaL_openlibs(S);

  if (luaL_loadfile(S, filename) || lua_pcall(S, 0, 0, 0))
    script_bail (S, "Can't load %s into memory\n", filename);

  return S;
}

void print_boxes()
{
  struct Box *box;

  printf("index: %d\n", box_index);

  int i, j;
  for (i = 0; i < box_index; i++) {
    box = &boxes[i];

    printf("%p: %s => {", box, box->name);

    for (j = 0; j < box->count; j++) {
      printf(" %s ", box->files[j]);
    }

    printf("}\n");
  }
}

int main (void)
{
  lua_State * S = load_lua_file ("boxes.lua");
  load_boxes(S);
  lua_close(S);

  print_boxes();

  lua_State * N = load_lua_file ("boxes.lua");
  lua_close(N);

  print_boxes();

  return 0;
}

boxes = { 
  { name = "first", files = { "one.lua" } },
  { name = "second", files = { "two.lua", "three.lua", "four.lua" } }
};

Here is the output I am getting:
0x10f985330: first => { one.lu }
0x10f985368: second => { two.lu  three.lua  four.lua }

0x10f985330: second => { D }
0x10f985368: D => { J?????  ?
                        (?   }

I want to note that this works sporadically. Sometimes it prints the same data twice (hinting that it is working). Most of the time I get the above. What am I doing wrong that causes memory for the array to become corrupted?

A:

It seems that you make a pointer to a block of memory that is deallocated at some point, I think here
lua_close(S);

Then when you try to access files or name in the structures the pointers point to garbage.
You need to copy the strings, you can use strdup() or any equivalent function for that, every string for which you save a pointer, must still be valid when you print the boxes.
A better solution would be not to deallocate the memory allocated by the lua library before printing the boxes, you can do that by moving the lua_close(S); after the print_boxes() call.
Because if you use strdup() then you must call free() on the pointers after you don't need to access them anymore, that would not work directly because the poitners are qualified with const, so if you use strdup() you need to

Remove const from the struct members definition.
Call free() on every pointer that points to a value returned by strdup().

