Hi everybody. I'm trying to simulate memory allocation using a best-fit strategy and I can't get rid of this annoying bug. Can you help me?

Here is my overall simplified strategy:
  1. Create an array of ones and zeros, where 1=used byte, 0=free byte.
  2. Scan that array for blocks of contiguous zeros (i.e. available memory).
  3. Given a process' required bytes, determine which of the above blocks is the best fit.
  4. Simulate the memory allocation for that process by flipping the array's zeros to ones.


The problem is that I get a bug when I test it. After the allocation for the first process is done, the subsequent allocations do not go through. The variable "best" stays at 9999 which indicates to me that some variable or array is not getting updated. The problem is that I don't know which one and I've been trying to figure this out for a day now.

Here is what I have so far. The code is well-commented and should be straight-forward. I left some print statements that I've been using for debugging:
Code:
/*
 * File: memory_bf.c
 * Date: 12/Dec/09
 * Author: Rommel Rico
 * Contents: Allocates memory under a Best-Fit Strategy.
 */

#include "binheap.h"
#include <stdio.h>
#include <stdlib.h>

#define MAXMEMORY 10000 //Maximum number of bytes in memory.
#define INFINITY 99999 //Unreachable number.
int memoryArrayBF[MAXMEMORY] = {0}; //Used to determine available memory. 0=free byte. 1=used byte.
int idArrayBF[MAXMEMORY] = {0}; //Stores which processes are in memory and where.
int memBlocks[100] = {0}; //Stores the size of contiguous available memory.
int memBlocksID[100] = {0}; //Stores the id of a memory block.

/* Structure defining an Event. */
struct Event{
  int id; //To store the process id.
  int event_type; //Could be 0 (departure) or 1(arrival).
  int bytes; //Stores the bytes of process.
  double event_time; //Stores the time an event occurs.
  double run_time; //Stores the run time of an event.
};



/* Allocate Memory in a Best-Fit fashion. */
int AllocateMemoryBF(struct Event X){
  //Scan the memoryArray for available blocks of memory.
  int i,j = 0;
  int count = 0;
  for(i=0; i<MAXMEMORY; i++){
    if(memoryArrayBF[i]==0){ //found unused memory.
      count++;
    }
    else if(memoryArrayBF[i]==1 && count>0){ //found allocated memory.
      memBlocks[j]=count;
      memBlocksID[j]=i-(count-1);
      printf("ASSIGNED %d to MEMBlocks[%d].\n", count, j);
      j++;
      count=0; //reset count.
    }
    if(count==MAXMEMORY-1){ //Entire memory is available.
      printf("Entire memory is available.\n");
      printf("ASSIGNED %d to MEMBlocks[0].\n", count);
      memBlocks[0]=count;
      memBlocksID[0]=0;
      count=0; //reset count.
    }
  }

  //Scan the memBlocks for the best fit.
  int best = INFINITY;
  int bestID= 0;
  for(i=0;i<100;i++){
    if(memBlocks[i]>=X.bytes && memBlocks[i]<best){
      best=memBlocks[i];
      bestID=i;
    }
  }
  printf("After scanning memBlocks, best fit is: %d.\n", best);

  //Allocate the memory.
  if(best==INFINITY){
    printf("NO MEMORY.\n");
    return 1; //No memory available.
  }
  else{
    int place = memBlocksID[bestID];
    printf("ALLOCATED memory in index %d.\n", place);    
    for(i=place; i<(place+X.bytes); i++){
      memoryArrayBF[i] = 1; //allocate memory.
      idArrayBF[i] = X.id;
      printf("WROTE %d to memoryArrayBF[%d] and id=%d.\n", memoryArrayBF[i], i, X.id);
    }
    return 0; //Memory was succesfully allocated.
  }
}



/* Deallocate Memory for a Best-Fit fashion. */
int DeallocateMemoryBF(struct Event X){
  int i=0;
  for(i; i<MAXMEMORY;i++){
    if(idArrayBF[i]==X.id){
      memoryArrayBF[i] = 0; //free the memory.
      idArrayBF[i] = 0; //delete the process id.
    }
  }
  return 0;
}
I really appreciate your help. I know its something simple, but I just can't find the bug.