From: Bradley E. Smith (brad@bradley.bradley.edu)
Date: 02/18/93


From: brad@bradley.bradley.edu (Bradley E. Smith)
Subject: malloc bug?
Date: Thu, 18 Feb 1993 15:14:01 GMT

i am not sure I have seen this (there are alot of postings :-) ), but
anyways here goes. I was writing a function for a graphics class and
rather than use a file I thought to use malloc, now the below program
runs on my sun but dies on linux.....please don't get on me about
1 byte mallocs, it was my first pass at the program. But that not
with standing the program still dies.

#include <stdio.h>

struct production {
        char value;
        char *result;
};

#define C_MALLOC(c, x) if((c = malloc(x)) == NULL) {\
                                perror("malloc");\
                                exit(1);}

/* does the axiom to production with only 1 production rule */
char *
generate1(level,axiom,prd, heading)
int level;
char *axiom;
struct production prd;
double heading;
{
        extern char *malloc(), *realloc();
        int i, size_of_prd, j;
        char *cpt1, *cpt2, *cpt3, *cpt4;

        size_of_prd = strlen(prd.result)+1; /* how big is the trans. */

        cpt3 = malloc(strlen(axiom)+1); /* inital setting */
        cpt2 = NULL;

        strcpy(cpt3, axiom);
        for(i = 0; i < level ; i++ ) {
                printf("starting level %d\n", i);
                while(*cpt3) {
                        if(*cpt3 == prd.value) {
                                /* need to add the new value to */
                                if(cpt2 == NULL) {
                                        C_MALLOC(cpt2,size_of_prd)
                                        strcpy(cpt2,prd.result);
                                } else {
                                        /* need to add it to */
                                        j = strlen(cpt2) + size_of_prd + 1;
printf("j=%d,", j);fflush(stdout);
                                        C_MALLOC(cpt4,j)
                                        strcpy(cpt4,cpt2);
                                        strcat(cpt4,prd.result);
                                        free(cpt2);
                                        cpt2 = cpt4;
                                }
                        } else {
                                if(cpt2 == NULL) {
                                        C_MALLOC(cpt2,1)
                                        strncpy(cpt2,cpt3,1);
                                } else {
                                        j = strlen(cpt2) + 1 + 1;
printf("j=%d,", j);fflush(stdout);
                                        C_MALLOC(cpt4,j)
                                        strcpy(cpt4,cpt2);
                                        strncat(cpt2,cpt3,1);
                                        free(cpt2);
                                        cpt2 = cpt4;
                                }
                        }
                        cpt3++; /* look at next char */
                }
                /* at this point we need to swap the values */
                free(cpt3); /* get rid of old space */
                /* now cpt2 the current out will be come cpt3 on the
                   new loop
                */
                cpt3 = cpt2;
                cpt2 = NULL; /* start clean on new loop */
        }
        /* when we get here cpt3 has what we want */
        return(cpt3);
}

main()
{
        extern char *generate1();
        struct production pd;
        extern char *malloc();
        char *cp;

        pd.value = 'F';
        pd.result = "F+F-F-FFF+F+F-F";
        cp = generate1(5,"F+F+F+F", pd, 1.0);
        printf("cp = '%s'\n", cp);
}

-- 
Bradley Smith                    brad@bradley.edu ---  309-677-2337
Network & Technical Services @ Bradley University, Peoria, IL

"It's amazing how much scrap metal you get from 4 cans of beer"