Problem p00640 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Button {
  int left, right, top, under, link_index;
  char to_name[21];
};

struct Page {
  char name[21];
  vector<Button> B;
};

int num_page, W, H;
int *buffer;

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void func() {

  Page page[101];

  int pointer, buffer_size;

  int num_button;

  for (int i = 0; i < num_page; i++) {
    scanf("%s %d", page[i].name, &num_button);

    for (int k = 0; k < num_button; k++) {
      Button button;
      scanf("%d %d %d %d %s", &button.left, &button.top, &button.right,
            &button.under, button.to_name);
      page[i].B.push_back(button);
    }
  }

  for (int i = 0; i < num_page; i++) {
    for (int k = 0; k < page[i].B.size(); k++) {
      for (int p = 0; p < num_page; p++) {
        if (strCmp(page[i].B[k].to_name, page[p].name)) {
          page[i].B[k].link_index = p;
          break;
        }
      }
    }
  }

  buffer[0] = 0;
  pointer = 0;
  buffer_size = 1;

  int M, tmp_x, tmp_y;
  scanf("%d", &M);

  char order[10];

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", order);

    switch (order[0]) {
    case 'c':
      scanf("%d %d", &tmp_x, &tmp_y);

      for (int i = 0; i < page[buffer[pointer]].B.size(); i++) {
        if (tmp_x >= page[buffer[pointer]].B[i].left &&
            tmp_x <= page[buffer[pointer]].B[i].right &&
            tmp_y >= page[buffer[pointer]].B[i].top &&
            tmp_y <= page[buffer[pointer]].B[i].under) {
          buffer_size = pointer + 1;
          buffer[buffer_size] = page[buffer[pointer]].B[i].link_index;
          buffer_size++;
          pointer++;
          break;
        }
      }
      break;
    case 'b':
      if (pointer > 0)
        pointer--;
      break;
    case 'f':
      if (pointer < buffer_size - 1)
        pointer++;
      break;
    case 's':
      printf("%s\\n", page[buffer[pointer]].name);
      break;
    }
  }
}

int main() {

  buffer = new int[50000];

  while (true) {
    scanf("%d", &num_page);
    if (num_page == 0)
      break;

    scanf("%d %d", &W, &H);

    func();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Button {
  int left, right, top, under, link_index;
  char to_name[21];
};

struct Page {
  char name[21];
  vector<Button> B;
};

int num_page, W, H;

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void func() {

  Page page[101];

  int pointer;
  vector<int> buffer;

  int num_button;

  for (int i = 0; i < num_page; i++) {
    scanf("%s %d", page[i].name, &num_button);

    for (int k = 0; k < num_button; k++) {
      Button button;
      scanf("%d %d %d %d %s", &button.left, &button.top, &button.right,
            &button.under, button.to_name);
      page[i].B.push_back(button);
    }
  }

  for (int i = 0; i < num_page; i++) {
    for (int k = 0; k < page[i].B.size(); k++) {
      for (int p = 0; p < num_page; p++) {
        if (strCmp(page[i].B[k].to_name, page[p].name)) {
          page[i].B[k].link_index = p;
          break;
        }
      }
    }
  }

  buffer.push_back(0);
  pointer = 0;

  int M, tmp_x, tmp_y;
  scanf("%d", &M);

  char order[10];

  for (int loop = 0; loop < M; loop++) {
    scanf("%s", order);

    switch (order[0]) {
    case 'c':
      scanf("%d %d", &tmp_x, &tmp_y);

      for (int i = 0; i < page[pointer].B.size(); i++) {
        if (tmp_x >= page[pointer].B[i].left &&
            tmp_x <= page[pointer].B[i].right &&
            tmp_y >= page[pointer].B[i].top &&
            tmp_y <= page[pointer].B[i].under) {

          while (buffer.size() > pointer + 1) {
            buffer.erase(buffer.begin() + buffer.size());
          }
          buffer.push_back(page[pointer].B[i].link_index);
          pointer++;
          break;
        }
      }
      break;
    case 'b':
      if (pointer > 0)
        pointer--;
      break;
    case 'f':
      if (pointer < buffer.size() - 1)
        pointer++;
      break;
    case 's':
      printf("%s\\n", page[pointer].name);
      break;
    }
  }
}

int main() {

  while (true) {
    scanf("%d", &num_page);
    if (num_page == 0)
      break;

    scanf("%d %d", &W, &H);

    func();
  }

  return 0;
}