【发布时间】:2019-11-23 19:26:47
【问题描述】:
当我尝试运行我的代码时,出现警告,我不知道为什么....请帮助我,这是我的代码:
#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++;
}
}
我收到以下警告:
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);
^
是不是因为 GCC 版本不同?我该如何解决?这是C编程,我收到了两个警告,例如警告:从不兼容的指针类型赋值[默认启用]和从不同大小的整数转换为指针[-Wint-to-pointer-cast]。
【问题讨论】:
-
什么是
MyMalloc? -
它是另一个c文件,我有mymalloc.c和mymalloc.h,我尝试使用make来运行它。
-
你不包括
mymalloc.h。 -
我在 mymalloc.c 中包含了 mymalloc.h,这个文件是 memgrind.c,这个文件包含我的内存测试和分析代码,如上所述。所以三个文件:mymalloc.c、mymalloc.h 和 memgrind.c。我有一个makefile 有memgrind:gcc -g -O0 memgrind.c mymalloc.c -o memgrind 2>log.txt
-
MyMalloc函数的定义在哪里?你的MyMalloc可能会返回一个大小小于指针的值。假设它返回int,它是 4 个字节,而您的系统是 64 位,那么指针必须是 8 个字节。所以警告在那里发生。
标签: c