int main(int argc, char *argv[]) {
    // Check arguments
    if (argc < 2) {
        printf("Wrong arguments\n");
        return EXIT_FAILURE;
    }
    int maxnum = atoi(argv[1]);
    char *filename = argv[2];
    // Create new heap
    heap *theHeap = newHeap(maxnum);

    // Read lines of file
    FILE *fp = fopen(filename, "rt");
    while(!feof(fp)) {
        char buffer[256];
        fgets(buffer, 256, fp);
        if (feof(fp)) break;
        int value = atoi(buffer);
	insertHeap(theHeap, value);
        if (isFullHeap(theHeap)) {
            removeHeap(theHeap);
        }
    }

    // Show contents
    while(!isEmptyHeap(theHeap)) {
        int value = removeHeap(theHeap);
        printf("%d\n", value);
    }
    
    // Delete heap
    deleteHeap(theHeap);

    return EXIT_SUCCESS;
}
示例#2
0
文件: MemHeap.cpp 项目: tabchas/ee312
void deallocateMemory(void *p)
{
    /* since the available memory is one position above the signature
     * the signature can be found at one position below the first
     * byte of available memory
     */
    int *chunk_address = ((int *)p) - 1;
    unsigned chunk;
    int top_signature;
    int bottom_signature;
    int size;

    if ((chunk_address < memory_pool)
        || (chunk_address >= &memory_pool[pool_size]))
    {
        /* due to silliness in the MetroWerks compiler, it is possible
         * for new to be called before the memory heap is on-line.  In that
         * case the system default heap is used, and not this one.
         * this generally only happens for global constructors, such as
         * the stuff associated with cout and cin.
         * When these objects are destroyed (after main ends) the
         * runtime system calls delete on all these things but
         * since our heap is now online, we get all the other heap's memory
         * back.  We're just going to ignore it.  The OS will clean up
         * that memory anyway when the application quits, so its
         * no big deal.
         */

        /* if you want to see this in action, de-comment the following line */
        // cout << "hey! that ain't mine, don't throw that junk at me\n";
        return;
    }
    chunk = chunk_address - memory_pool;

    bottom_signature = memory_pool[chunk];
    top_signature = memory_pool[topSignature(chunk)];

    /* another sanity check, not really insane at all. */
    assert(bottom_signature == top_signature
           && bottom_signature < 0);

    size = -bottom_signature;
    setBothSignatures(chunk, size);

    mergeChunkIfOK(chunk);

    if (chunk > 0)
    { // this is not quite silly, chunk could be equal to zero
        unsigned previous_chunk = predecessorChunk(chunk);
        mergeChunkIfOK(previous_chunk);
    }

    if (mem_leak_check && isEmptyHeap())
    {
        std::cout << "Yay, you do not have a memory leak!" << std::endl;
    }
}
示例#3
0
 ~CheckMemoryLeaks(void) {
     if (! isSaneHeap()) {
         printf("oh goodness! you've corrupted the heap, naughty naughty\n");
         return;
     }
     if (! isEmptyHeap()) {
         printf("Uh Oh! you have a memory leak somewhere, better find it\n");
         return;
     }
     printf("The heap is all clean, good work!\n");
 }
示例#4
0
文件: testheap.c 项目: dougvk/CS223
// -------------------------------------------------------------------------
int main (int argc, const char * argv[]) 
{
  int* key;
  char command;
  bool quit=false;
  printf( "Welcome to heap test program\n" );
  Heap h = newHeap( comp );
  do {
    printf( "Command: ('h' for help): " );
    scanf( " %c", &command );
    switch( toupper(command) ) {
    case 'H':			/* help */
      printf( "Commands:\n"
	      "  i num - insert number into heap\n"
	      "  d     - delete minimum element from heap\n"
	      "  p     - print heap\n"
	      "  q     - quit\n" );
      break;
    case 'I':			/* insert */
      key = safe_malloc( sizeof *key );
      scanf( "%d", key );
      insertHeap( h, key );
      break;
    case 'D':			/* delete_min */
      if (isEmptyHeap( h )) {
	printf( "Heap is empty\n" );
      }
      else {
        key = deleteMinHeap( h );
        printf( "%d\n", *key );
        free( key );
      }
      break; 
    case 'P':			/* print */
      mapHeap( print_element, h, NULL );
      printf( "\n" );
      break;
    case 'Q':
      quit=true;
    }
  } while( !quit );
  
  freeHeap( h );
  printf( "Goodbye!\n" );
}
void testStage4(void) {
	char p[20];
	if (! isSaneHeap()) {
		printf("oh goodness! you've corrupted the heap, naughty naughty\n");
		return;
	}
	if (! isEmptyHeap()) {
		printf("Uh Oh! you have a memory leak somewhere, better find it\n");
		return;
	}	
	/* if we reach this point, the heap is OK */
	printf("woo HOO! the heap is OK, test 4 looks good so far, now we're going to crash...\n");
	/* each of the following lines should crash the program (an assert should fail) 
	 * try each of them in turn to make sure you're catching the obvious mistakes 
	 * just uncomment the line and see what happens (it should crash) 
	 */
	 printf("crashing with utstrlen\n\n\n"); utstrlen("Hello World");	
	 printf("crashing with utstrcpy\n\n\n"); utstrcpy(p, "Hello World");
	 printf("crashing with utstrcat\n\n\n"); utstrcat(p, "Hello World");
	 printf("crashing with utstrfree\n\n\n"); utstrfree((char *)malloc(20));
	 printf("crashing with utstrrealloc\n\n\n"); utstrrealloc((char *)malloc(20), 40);
}