Reputation: 3
when i try to run my code, there got warning and i don't know why....Please help me, this is my code:
#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#define TESTTIMES 2000
void test1()
{
// printf("--------TEST1----------\n");
int i = 0;
for (i = 0; i < 150; i++) {
char *p = (char*)MyMalloc(1);
MyFree(p);
}
}
void test2()
{
// printf("--------TEST2----------\n");
int *a[150], i;
for (i = 0; i < 150; i++) {
char *p = (char*)MyMalloc(1);
a[i] = p;
if ((i + 1) % 50 == 0) {
int j = i + 1 - 50;
while (j <= i) {
MyFree(a[j]);
j += 1;
}
}
}
}
void test3()
{
// printf("--------TEST3----------\n");
int flag, cnt = 0, top = 0;
char *b[50];
srand(time(NULL));
while(cnt < 50) {
flag = rand() % 2;
if (flag) {
char *p = (char*)MyMalloc(1);
b[top] = p;
top++;
cnt += 1;
} else {
if (top > 0) {
top -= 1;
MyFree(b[top]);
}
}
}
while (top > 0) {
top--;
MyFree(b[top]);
}
}
void test4()
{
// printf("--------TEST4----------\n");
int flag, size, cnt = 0, top = 0;
char *b[50];
srand(time(NULL));
while(cnt < 50) {
flag = rand() % 2;
size = rand() % 64 + 1;
if (flag) {
char *p = (char*)MyMalloc(size);
b[top] = p;
top++;
cnt += 1;
} else {
if (top > 0) {
top -= 1;
MyFree(b[top]);
}
}
}
while (top > 0) {
top--;
MyFree(b[top]);
}
}
void test5()
{
// printf("--------TEST5----------\n");
int i = 0;
while (i < TESTTIMES) {
char *p = MyMalloc(4096);
MyFree(p);
i++;
}
}
void test6()
{
// printf("--------TEST6----------\n");
int top = 0, i = 0;
char *b[100];
while(1) {
char *p = (char*)MyMalloc(100);
if (p) {
b[top] = p;
top++;
} else {
break;
}
}
while(i < top){
MyFree(b[i]);
i++;
}
}
and I got warning on these lines:
ind.c: In function \u2018test1\u2019:
ind.c:79:19: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
char *p = (char*)MyMalloc(1);
^
ind.c: In function \u2018test2\u2019:
ind.c:90:19: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
char *p = (char*)MyMalloc(1);
^
ind.c:91:14: warning: assignment from incompatible pointer type [enabled by default]
a[i] = p;
^
ind.c: In function \u2018test3\u2019:
ind.c:112:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
char *p = (char*)MyMalloc(1);
^
ind.c: In function \u2018test4\u2019:
ind.c:140:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
char *p = (char*)MyMalloc(size);
^
ind.c: In function \u2018test5\u2019:
ind.c:162:19: warning: initialization makes pointer from integer without a cast [enabled by default]
char *p = MyMalloc(4096);
^
ind.c: In function \u2018test6\u2019:
ind.c:176:19: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
char *p = (char*)MyMalloc(100);
^
is it because of different GCC version? How can I fix it? It is C programming, I got two warnings such as warning: assignment from incompatible pointer type [enabled by default] and cast to pointer from integer of different size [-Wint-to-pointer-cast].
Upvotes: 0
Views: 95
Reputation: 225767
The reason you're getting these errors:
warning: cast to pointer from integer of different size
Is because your main file doesn't know how MyMalloc
is defined, so it defaults to a function returning an int
. You can fix this by putting #include "mymalloc.h"
at the top of your main file.
If you do this, you'll see another error, specifically that buffer
and memory
are defined twice. This is because you're defining these two variables in your header file. It is for this reason that variables shouldn't be defined in a header file.
Since you only use buffer
and memory
in mymalloc.c, you should move them into that file. That way you avoid the multiple definition error. You should also move struct node
for the same reason.
Upvotes: 0