gpt4 book ai didi

c - 警告 : assignment from incompatible pointer type [enabled by default] and cast to pointer from integer of different size [-Wint-to-pointer-cast]

转载 作者:行者123 更新时间:2023-11-30 16:09:50 26 4
gpt4 key购买 nike

当我尝试运行我的代码时,出现警告,我不知道为什么......请帮助我,这是我的代码:

#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]。

最佳答案

您收到这些错误的原因:

warning: cast to pointer from integer of different size

是因为你的主文件不知道MyMalloc是如何定义的,所以它默认为返回int的函数。您可以通过将 #include "mymalloc.h" 放在主文件的顶部来解决此问题。

如果这样做,您将看到另一个错误,特别是 buffermemory 被定义了两次。这是因为您在头文件中定义了这两个变量。正是由于这个原因,变量不应该定义在头文件中。

由于您在 mymalloc.c 中仅使用 buffermemory,因此您应该将它们移至该文件中。这样就可以避免多重定义错误。出于同样的原因,您还应该移动struct node

关于c - 警告 : assignment from incompatible pointer type [enabled by default] and cast to pointer from integer of different size [-Wint-to-pointer-cast],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com