简体   繁体   English

错误:“c中未分配指针”

[英]error: “pointer being freed was not allocated” in c

this error is always fired, when i'm try to free my allocated struct the second time, which it shouldn't, because the struct is set to NULL after i'm freeing it. 这个错误总是被触发,当我试图第二次释放我分配的结构时,它不应该,因为结构在释放后被设置为NULL。

here's my struct with no real pointer inside it: 这是我的结构,里面没有真正的指针:

 typedef struct{
        int frame; 
        double timestamp; 
        int identifier; 
        int state; 
        int unknown1; 
        int unknown2; 
        mtVector normalized;
        float size; 
        int unknown3;
        float angle; 
        float majorAxis;
        float minorAxis;
        mtVector unknown4; 
        int unknown5[2]; 
        float unknown6; 
    }Touch;

the barebone main function: 准系统主要功能:

int main(){
    Touch *myTouch = NULL;
    int inputCounter = 0;
    //whenever  a touch is recognized:
    ...
    myTouch = (Touch*)realloc(myTouch,sizeof(Touch)*(inputCounter++));
    ...
    // everything works fine until:
    freeTouch(myTouch);
}

void freeTouch(Touch *f){
    if(f != NULL){
        free(f);
        f = NULL;
    }
}

anybody got an idea? 有人有个主意吗?

f is a local variable. f是局部变量。 free(f) will affect the allocated memory, but f = NULL has no impact on myTouch in freeTouch(myTouch); free(f)会影响分配的内存,但f = NULLfreeTouch(myTouch); myTouch没有影响freeTouch(myTouch); .

Try 尝试

void freeTouch(Touch **f){
    if(*f != NULL){
        free(*f);
        *f = NULL;
    }
}

instead and use freeTouch(&myTouch) . 而是使用freeTouch(&myTouch)

You have two problems there. 你有两个问题。 The first is that it's not a good idea to explicitly cast the return value from malloc or realloc . 首先,从mallocrealloc显式转换返回值不是一个好主意。 Doing so can cause problems if you forget to include the prototype/header for it. 如果您忘记包含原型/标题,这样做可能会导致问题。

Secondly, freeing f within the function frees the local copy. 其次,在函数中释放f可以释放本地副本。 Until C gains references, there are two possibilities. 在C获得参考之前,有两种可能性。 First pass a pointer to the pointer and use that: 首先传递一个指向指针的指针,然后使用:

void freeTouch (Touch **pF){
    if (*pF != NULL){
        free (*pF);
        *pF = NULL;
    }
}
:
freeTouch (&myTouch);

or pass back NULL so you can assign: 或传回NULL,以便您可以分配:

void *freeTouch (Touch *f){
    free (f);
    return NULL;
}
:
myTouch = freeTouch (myTouch);

You'll notice that the second one doesn't care whether you pass in NULL - it's perfectly acceptable to try an free the NULL pointer since it's effectively a no-op (other than the function call itself). 你会注意到第二个并不关心你是否传入NULL - 尝试释放NULL指针是完全可以接受的,因为它实际上是一个无操作(除了函数调用本身)。

First of all, never use 首先,永远不要使用

x = realloc(x, size);

because if x is allocated before and realloc fails, you make it NULL while the memory is still there and therefore you create garbage. 因为如果在之前分配x并且realloc失败,则在内存仍然存在时将其NULL ,因此您将创建垃圾。

Second, 第二,

void freeTouch(Touch *f);

gets a pointer by value and therefore cannot change the pointer itself. 按值获取指针,因此无法更改指针本身。 So your f = NULL; 所以你的f = NULL; is not effective. 没有效果。 You need to change your code to: 您需要将代码更改为:

int main(){
    Touch *myTouch = NULL, temp;
    int inputCounter = 0;
    //whenever  a touch is recognized:
    ...
    temp = realloc(myTouch,sizeof(*temp) * (inputCounter++));
    if (temp == NULL)
        /* handle error */
    myTouch = temp;
    ...
    // everything works fine until:
    freeTouch(&myTouch);
}

void freeTouch(Touch **f){
    if(f != NULL && *f != NULL){
        free(*f);
        *f = NULL;
    }
}

Sidenote: It's a good idea to use realloc (and likewise malloc ) like this: 旁注:使用realloc (以及类似malloc )是个好主意,如下所示:

x = realloc(count * sizeof(*x));

There is no need to cast the output or realloc . 无需转换输出或realloc Also, sizeof(*x) allows you to not repeat the type of x every time. 此外, sizeof(*x)允许您不要每次都重复x的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM