alloc.h

Has anyone used CodeBlocks (MingW plugins) here? I can't seem to use the alloc.h library. When I try to compile my code, the compiler can't seem to find the alloc.h...

1
2
3
4
5
6
\Desktop\Proj\LinkedList\main.c|4|error: alloc.h: No such file or directory|
\Desktop\Proj\LinkedList\main.c||In function 'insertData':|
\Desktop\Proj\LinkedList\main.c|84|warning: incompatible implicit declaration of built-in function 'malloc'|
\Desktop\Proj\LinkedList\main.c||In function 'delete':|
\Desktop\Proj\LinkedList\main.c|114|warning: incompatible implicit declaration of built-in function 'free'|
||=== Build finished: 1 errors, 2 warnings ===|


I tried compiling the code using Turbo C (in DOSBox), and at first it was sending out a "Declaration not allowed here error":

1
2
3
4
5
6
7
8
9
10
11
12
13
void save() {
	int i;
	LIST *p;
	p = L;
	FILE *fp;
	fp = fopen("/mit594.dbf", "w+");
	
	while (p != NULL) {
		fprintf(fp,"%d\n",L->x);
		p=p->next;
	}
	fclose(fp);
}


I tried moving the FILE *fp line to the first line in the code and it worked. Can someone explain to me why this happened?
http://stackoverflow.com/questions/9513604/declare-c89-local-variables-in-the-beginning-of-the-scope

> I can't seem to use the alloc.h library
¿what's that? ¿what do you want to use from there?


malloc() and free() are in stdlib.h
Well, I'll be using pointers. Like so:

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
typedef struct list {
	int x;
	struct list *next;
	} LIST;

LIST *L;

void insert(int y) { 
	LIST *q, *p, *new;
	q = p = L;
	new = (LIST *)malloc(sizeof(LIST));
	new->x = y;

	while (p != NULL) {
		q = p;
		p = p->next;

		if (L == p) {
			L = new;
		} else {
			q->next = new;
		}

		new->next = p;
	}
}

void delete(int y) {
	LIST *q, *p;
	p = q = L;
	while (p!= NULL && p->x != y) {
		q = p;
		p = p->next;
	}
	
	if (L == p) {
		L = p->next;
	} else {
		q->next = p->next;
	}
	free(p);
}


I'll try linking stdlib.h
"delete" is a reserved keyword, you should use another like "release" or "cleanup".
Last edited on
That looks like C, in which case it's ok. You just need to use a C compiler and not a C++ compiler.

As you're using MingW, make sure your files have a .c extension and use gcc and not g++.

According to the C standard, you need stdlib.h for malloc().
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
Last edited on
Topic archived. No new replies allowed.