problem free(buff);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
char *buff;
void minterm(int x)
{
	static int z=x,a=0;
	if(z==x)buff = (char*)malloc(x);
	for(int i=0;i<=1;i++)
	{
		buff[x-1] = (i+'0');
		if(x>1)
		{
			minterm(x-1);
		}
		else
		{
			buff[x-1]=(i+'0');
			buff[z]='\0';
			if(a==0)printf("MT %s\n",var);
			printf("%2d %s\n",++a,buff);
		//	free(buff);  <<-- problem
			
		}
	}
}

help me please..
i don't know how to free that variable
thx before..
Last edited on
There are so many things wrong with this code that it's hard to begin anywhere.
So instead, you should just explain what on earth you're trying to do and we can show you an acceptable solution.

Edit: even after your edit, the code is still not obvious. Don't use meaningless variable names like z, x or a, don't use static or global variables unless necessary, avoid recursion unless it's hard to do it with iteration, don't use malloc, free or printf in C++ code.
Last edited on
i've tried to make a truth table with recursion
because i need infinite variable depends on "int x"

it's the full code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;

char *var=NULL,*buff=NULL;

struct data
{
	int num;
	char var;
	struct data *next;
}*head,*tail,*curr;

void minterm(int x)
{
	static int z=x,a=0;
	if(z==x)buff = (char*)malloc(x);
	for(int i=0;i<=1;i++)
	{
		buff[x-1] = (i+'0');
		if(x>1)
		{
			minterm(x-1);
		}
		else
		{
			buff[x-1]=(i+'0');
			buff[z]='\0';
			if(a==0)printf("MT %s\n",var);
			printf("%2d %s\n",++a,buff);
			//free(buff);
		}
	}
}


void cetak()
{
	if(head)
	{
		curr = head;
		printf("\n");
		while(curr)
		{
			printf("%d %c\n",curr->num,curr->var);
			curr = curr->next;
		}
	}
}

void del()
{
	if(head)
	{
		while(curr)
		{
			curr = head;
			head = head->next;
			free(curr);
		}
		head = tail = curr = NULL;
	}
}

int main()
{
	int byk;
	printf("how much variable : ");
	scanf("%d",&byk);fflush(stdin);
	var = (char*)malloc(byk+1);
	for(int i=0;i<byk;i++)
	{
		printf("input variable ke-%d : ",i+1);
		scanf("%c",&var[i]);fflush(stdin);
		curr = (struct data*)malloc(sizeof(struct data));
		curr->var = var[i];
		curr->num = i+1;
		if(!head)
		{
			head = tail = curr;
		}
		else
		{
			if(head == tail)
			{
				head->next = tail->next = curr;
				tail = curr;
			}
			else
			{
				tail->next = curr;
				tail = curr;
			}
		}
		tail->next = NULL;
	}
	var[byk]='\0';
	minterm(byk);
	free(var);
	buff = NULL;
	cetak();
	del();
	getchar();
	return 0;
}




i made it for quine mccluskey algorithm
i've been search on internet for c code
but i just found in c++ code
for now, i just know c language..
then, i tried to made it by myself
thx for your attention ^^
Last edited on
If this is a C program, you should ask on a C forum. There people probably won't cry out in outrage when seeing such code.
In any case, you're freeing buff twice (for x=1 and x=0).
Last edited on
i think it's a c forum too, because there's a lot of c library.
sorry for my dumb question.

and thx, for your answer.
now i know how to free that variable.
Sorry but i confused what minterm is meant to do exactly. Could you post an explanation of just that function and why you have a problem freeing the buf variable. I mean, i can't immediately see anything syntactically wrong with that line but won't be able to find any logic issues if i don't know what it's meant to do
minterm is for make a truth table based on variable
and print (2^variable)x
for more, just compile and run
You should also note that buff[x-1] is an invalid memory access when x reaches 0 and for a C string of length x you need x+1 bytes (one for terminating null).
i think x never reaches 0, because i put if(x>1) instead.
and buff[1-1]=buff[0] what it's invalid memory access?
Yeah, then it's okay.

The same can be accomplished in a cleaner way when you put the actual recursive functionality in its own function. Without static variables you can now call the function more than once in your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void minterm_rec(char* buf,int pos,int* counter)
{
    for(int i=0;i<2;i++)
    {
        buf[pos] = i+'0';
        if (pos>0)minterm_rec(buf,pos-1,counter);
        else printf("%2d: %s\n",++*counter,buf);
    }
}

void minterm(int digits)
{
    char* str=(char*)malloc(digits+1);
    str[digits]=0;
    int counter=0;
    minterm_rec(str,digits-1,&counter);
    free(str);
}
Last edited on
wow..
that's cleaner code
anyway, i think that's str[digits] not str[x]

thx a lot
Aye, I forgot to replace that x. Fixed now.
thx, i have a new question about linked list
can i posted a new thread in this forum??
or i must find another forum
Technically most C programs are valid C++ programs.
If you ask your questions here you should explicitly mention that you're using C, though.
Still, I would think that you could get better help from fellow programmers on a C forum.
Topic archived. No new replies allowed.