Thread: Issues with free and dynamic memory

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    2

    Question Issues with free and dynamic memory

    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.

    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;
    }
    Dan S.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Set list to be NULL at the end of the free_up() function. The reason for your problem is that free_up() frees up the memory, so list is a dangling reference. One of the first things that load_string() does to list is pass it to realloc().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I get a fatal “address error”, and I have to completely clear the calculators memory.
    You need to do
    list = NULL;
    at the end of your free_up() function.

    Other problems
    > #include <alloc.h>
    malloc / realloc / free etc are declared in stdlib.h which you also include.
    alloc.h is an obsolete and non-portable header.

    There is no need to cast any of the results of malloc/calloc/free - see the FAQ

    I also can't see why you would need this cast either.
    load_string((char *)"dummy string");

    > if (strcpy(tmp_st,st)==NULL)
    strcpy never fails like this - it always returns the first parameter.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    2
    Thank you gentlemen, It works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM
  5. Dynamic memory confusion
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 06:15 PM