This is my first time working with C (worked with Fortran and VB for years), and I have run into an error that I can’t figure out. I’m working on a program for my ti-89 calculator that will read values from a file and then display. The code below is a simpler program that I wrote to help with debugging.
The problem:
Memory is allocated, and then freed, but when I try to re-allocate memory I get a fatal “address error”, and I have to completely clear the calculators memory.
I have been working on this for 4 days and I’m stumped; does anyone see an issue with the following code.
Dan S.Code:/*############################################################################*/ #include <stdlib.h> #include <graph.h> #include <kbd.h> #include <compat.h> #include <alloc.h> #include <string.h> /*############################################################################*/ unsigned short int key; unsigned short int max=0; unsigned short int i; char **list = NULL; /*############################################################################*/ void load_string(char *s); void free_up(); /*############################################################################*/ void _main(void) { while (key != 264){ load_string((char *)"dummy string"); free_up(); key = ngetchx(); } free_up(); } /*############################################################################*/ void load_string(char *st){ char **tmp; char *tmp_st; if(st != NULL){ max++; if (( tmp=(char **)realloc(list,max*sizeof(char *)) )==NULL){ // realloc failed, end program max--; free_up(); ClrScr(); DrawStr(0, 0,"System out of memory(1),", A_NORMAL); DrawStr(0, 10,"press any key to exit!", A_NORMAL); ngetchx(); exit(0); }else{ // try to allocate space for st list=tmp; if ((tmp_st=malloc(strlen(st) + 1))==NULL){ //no space to allocate list[max-1]=NULL; free_up(); ClrScr(); DrawStr(0, 0,"System out of memory(2),", A_NORMAL); DrawStr(0, 10,"press any key to exit!", A_NORMAL); ngetchx(); exit(0); }else{ list[max-1]=tmp_st; if (strcpy(tmp_st,st)==NULL){ //could not copy data, end program free_up(); ClrScr(); DrawStr(0, 0,"couldn't copy to memory,", A_NORMAL); DrawStr(0, 10,"press any key to exit!", A_NORMAL); ngetchx(); exit(0); } } } }else{ // st was null, end program free_up(); ClrScr(); DrawStr(0, 0,"Input was Null, press any", A_NORMAL); DrawStr(0, 10,"key to exit!", A_NORMAL); ngetchx(); exit(0); } } /*############################################################################*/ void free_up(void){ if (max > 0){ for (i=0;i<max;i++){ if (list[i] != NULL){ free((void *)list[i]); } } free ((void *)list); } max=0; }



LinkBack URL
About LinkBacks


