hi guys,
i need to do up a code to read a very big file 16M and also long line of like 32,000 characters and check for palindrome.
the code i came up with eat up 23M of memory in solaris5.
is there a memory leak somewhere?
Code:/* * Author: Chua Jie Sheng * Status: read, output, checking pali not done */ #include <stdio.h> #include <stdlib.h> #include <string.h> // definition /* * the higher the DEBUG_LEVEL, the more debug message to print * general guideline * 0 - no debug message * 1 - debug message * 2 - info */ //#define DEBUG_LEVEL 0 #define ARRAY_SIZE (10000) // data structure struct linked_list { char **text; int size; //int mismatch; }; typedef struct linked_list list; list *current; //list *longest; //list *shortest; //char debug_buffer[50]; int r, c; // prof protype void my_memusage(char *); // prototype //void debug(int lvl, char msg[]); int row(int index); int col(int index); list *create_list(); char *realloc_list(char *to_be_realloc, int size); char check_char(int c); void put2char(int c); int char_matcher(int index, int size, char** text); void print_string(); // developer methods /* void debug(int lvl, char msg[]) { if (lvl <= DEBUG_LEVEL) { printf("%s", msg); } } */ // data struc method int row(int index) { r = (index - (index % ARRAY_SIZE)) / ARRAY_SIZE; return r; } int col(int index) { c = index - (row(index) * ARRAY_SIZE); return c; } // create new linked_list node list *create_list() { list *created = (struct linked_list *)calloc(1, sizeof(list)); if (created == NULL) { // debug /* sprintf(debug_buffer, "unable to allocate memory.\n"); debug(1, debug_buffer); */ exit(1); } else { //created->text = (char **)malloc(1); //created->text[0] = (char *)malloc(ARRAY_SIZE * sizeof(char)); created->size = 0; //created->mismatch = 0; return created; } } // realloc memory to save space char *realloc_list(char *to_be_realloc, int size) { return (char *)realloc(to_be_realloc, size); } // check char char check_char(int c) { if (c > 31 && c < 127) { return (char) c; } return NULL; } // handle adding of new char array void put2char(int c) { // debug /* sprintf(debug_buffer, "c = %d; ", c); debug(2, debug_buffer); //printf("%d ", c); */ // when current == null or c == 10 // go to next linked list when is a newline if (c == 10 && current->size > 0) { // debug /* sprintf(debug_buffer, " -going next node.-\n"); debug(2, debug_buffer); //printf(" -going next node.-\n"); */ // check pali int mismatch = char_matcher(0, current->size, current->text); /* sprintf(debug_buffer, "mismatch: %d\n", mismatch); debug(1, debug_buffer); */ // // print if (mismatch == 0) { // there is no error // mismatch = 0 print_string(current); } /* sprintf(debug_buffer, "current->size: %d\n", current->size); debug(1, debug_buffer); */ // // discard int index = 0; for (; index < current->size; index=index+ARRAY_SIZE) { free(current->text[row(index)]); } free(current->text); free(current); // create new list current = create_list(); } char ch = check_char(c); if (ch != NULL) { // debug /* sprintf(debug_buffer, "at %d, ch = %c\n", current->size, ch); debug(1, debug_buffer); */ // is full if ((current->size - (row(current->size) * ARRAY_SIZE )) == 0) { // debug /* sprintf(debug_buffer, "at %d, creating next row.\n", current->size); debug(1, debug_buffer); */ // create next row current->text = (char **)realloc(current->text, row(current->size)+1); if (current->text) { } current->text[(row(current->size))] = (char *)malloc(ARRAY_SIZE * sizeof(char)); } // move next current->text[row(current->size)][col(current->size)] = ch; current->size++; } else { // debug /* sprintf(debug_buffer, "ch skipped.\n"); debug(1, debug_buffer); //printf("ch = %c\n", ch); */ } } // char matcher for text int char_matcher(int index, int size, char** text) { // return when reach middle if (index == size/2) { return 0; } // checking // return 0 if no mismatch // return 1 if there is mismatch int back = size-index-1; if (text[row(index)][col(index)] == text[row(back)][col(back)]) { return char_matcher(index+1, size, text); } else { return 1; } } // print string void print_string(list *list) { int index = 0; // loop the linked list for (; index < list->size; index=index+ARRAY_SIZE) { // printing only if there is text and is pali int col = index % ARRAY_SIZE; int row = index / ARRAY_SIZE; printf("%s", list->text[row]); } printf("\n"); } // only main method after this int main() { // prof command my_memusage("INIT"); int c; // note that c is int and not char current = create_list(); while ((c = getchar()) != EOF) { // exit when EOF put2char(c); /* copy input char to output */ } my_memusage("END"); // malloc space print_process_memorysize(); return(0); }



LinkBack URL
About LinkBacks



