can't free() and program terminate.

This is a discussion on can't free() and program terminate. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #define min_range 0 #define max_range 20 typedef struct { int k; int s; int f; ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    can't free() and program terminate.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define min_range 0
    #define max_range 20
    
    typedef struct {
    	int k;
    	int s;
    	int f;
    }sfType;
    
    void exchangeSF(sfType *sf1, sfType *sf2)
    {
    	sfType t = *sf1;
    	*sf1 = *sf2;
    	*sf2 = t;
    }
    
    void quicksort(sfType *sf, int p, int r)
    {
    	int q;
    	if (p < r) {
    		q = partition(sf, p, r);
    		quicksort(sf, p, q-1);
    		quicksort(sf, q+1, r);
    	}
    }
    
    int partition(sfType *sf, int p, int r)
    {
    	int i, j;
    	i = p-1;
    	for (j = p; j < r; j++) {
    		if (sf[j].f <=sf[r].f) {
    			i++;
    			exchangeSF(&sf[i], &sf[j]);
    		}
    	}
    	i++;
    	exchangeSF(&sf[i], &sf[r]);
    	return i;
    }
    
    void generation(sfType *sf, int n)
    {
    	FILE *fData;
    	int i;
    	srand(time(NULL));
    	for (i = 0; i < n; i++) {
    		sf[i].k = i;
    		sf[i].s = rand()%(max_range-min_range)+min_range;
    		sf[i].f = rand()%(max_range-sf[i].s)+(sf[i].s+1);
    	}
    	fData = fopen("activity-selection.dat", "wt");
    	fprintf(fData, "%d\n", n);
    	for (i = 0; i < n; i++) {
    		if (i % 10 == 0) fprintf(fData, "\n");
    		fprintf(fData, "%d ", sf[i].s);
    	}
    	fprintf(fData, "\n");
    	for (i = 0; i < n; i++) {
    		if (i % 10 == 0) fprintf(fData, "\n");
    		fprintf(fData, "%d ", sf[i].f);
    	}
    	fclose(fData);
    }
    
    void RecursiveActivitySelector(sfType *sf, int *index, int *w, int i, int j)
    {
    	int m;
    	m = i+1;
    	while ((m < j) && (sf[m].s < sf[i].f))
    		m++;
    	if (m < j) {
    		index[*(w++)] = sf[m].k;
    		RecursiveActivitySelector(sf, index, w, m, j);
    	}
    }
    
    void GreedyActivitySelector(sfType *sf, int *index, int *w, int n)
    {
    	int i, j;
    	i = 0;
    	*w = 1;
    	index[0] = sf[0].k;
    	for (j = 1; j < n; j++)
    		if (sf[j].s > sf[i].f) {
    			index[*(w++)] = sf[j].k;
    			i = j;
    		}
    }
    
    void outputData(sfType *sf, int *indexR, int *indexG, int r, int g, int n)
    {
    	FILE *fData;
    	int i;
    	fData = fopen("ActivitySelection.out", "wt");
    	fprintf(fData, "Using Dynamic Programming:\n");
    	for (i = 0; i <= r; i++) {
    		fprintf(fData, "%d ", sf[indexR[i]].s);
    		if (i % 10 == 0) fprintf(fData, "\n");
    	}
    	for (i = 0; i <= r; i++) {
    		fprintf(fData, "%d ", sf[indexR[i]].f);
    		if (i % 10 == 0) fprintf(fData, "\n");
    	}
    	fprintf(fData, "\nUsing Greedy Alogrithm:\n");
    	for (i = 0; i <= g; i++) {
    		fprintf(fData, "%d ", sf[indexG[i]].s);
    		if (i % 10 == 0) fprintf(fData, "\n");
    	}
    	for (i = 0; i <= g; i++) {
    		fprintf(fData, "%d ", sf[indexG[i]].f);
    		if (i % 10 == 0) fprintf(fData, "\n");
    	}
    	fclose(fData);
    }
    
    int main()
    {
    	int *indexR, *indexG;
    	int r, g, i, n;
    	sfType *sf;
    	printf("n = ");
    	scanf("%d", &n);
    	sf = malloc(n*sizeof(*sf));
    	indexR = malloc(n*sizeof(*indexR));
    	indexG = malloc(n*sizeof(*indexG));
    	for (i = 0; i < n; i++) {
    		indexR[i] = 0;
    		indexG[i] = 0;
    	}
    	generation(sf, n);
    	quicksort(sf, 0, n);
    	r = 0;
    	g = 0;
    	RecursiveActivitySelector(sf, indexR, &r, 0, n);
    	GreedyActivitySelector(sf, indexG, &g, n);
    	printf("asdfasd\n");
    	outputData(sf, indexR, indexG, r, g, n);
    	free(sf);
    	free(indexR);
    	free(indexG);
    	return 0;
    }
    Why was the program terminated at RecursiveActivitySelector(sf, indexR, &r, 0, n) and GreedyActivitySelector(sf, indexG, &g, n) ??? Sometime GCC said that "free(): invalid pointer 0x804a018!".

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > index[*(w++)] = sf[m].k;
    This should probably be:
    index[(*w)++] = sf[m].k;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free() is killing my program?
    By alwut in forum C Programming
    Replies: 12
    Last Post: 12-24-2007, 02:50 PM
  2. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. How to free memory in this program
    By Coconut in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 10:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21