【问题标题】:warning: assignment from incompatible pointer type [enabled by default] and cast to pointer from integer of different size [-Wint-to-pointer-cast]警告:从不兼容的指针类型赋值 [默认启用] 并从不同大小的整数转换为指针 [-Wint-to-pointer-cast]
【发布时间】: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


【解决方案1】:

您收到这些错误的原因:

警告:从不同大小的整数转换为指针

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

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

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

【讨论】:

  • 谢谢你,我只是将内存和结构节点移动到 memgrind.c,它只有一个警告,在函数 "test2" 中,警告:分配来自不兼容的指针类型 [默认启用] a[ i] = p;
  • @galaboomboom a[i] 是一个 int * 并且您正在尝试为它分配一个 char *。类型需要匹配。
猜你喜欢
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-10
  • 2015-06-04
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多