简体   繁体   English

为什么会出现错误无效指针?

[英]Why do I get error invalid pointer?

/* DECLARED FUNCTIONS */
char *DetectDevice(void);

int main(int argc, char *argv[])
{
    char *PathToDevice;
    PathToDevice = DetectDevice();

...

    if(close(fd) == -1)
    {
        printf("Error Closing Port");
    }else
    {
        printf("whihi!");
        free(PathToDevice);
    }

    return 0;
}

char *DetectDevice(void)
{   
    char *Usbs = malloc(1024);
    Usbs = "/dev/ttyUSB1";
    return Usbs;
}

Error Message: * glibc detected * ./test: free(): invalid pointer: 0xbec1b504 错误消息: *检测到glibc * ./test:free():无效的指针:0xbec1b504

by the way... this program is compiled on a raspberry pi! 顺便说一下...这个程序是在树莓派上编译的!

char *DetectDevice(void)
{   
    char *Usbs = malloc(1024);
    Usbs = "/dev/ttyUSB1";
    return Usbs;
}

In char *DetectDevice(void) you are assigned "/dev/ttyUSB1" string address to Usbs that you returns. char *DetectDevice(void)你被分配"/dev/ttyUSB1"字符串地址Usbs ,你的回报。 and try to free. 并尝试免费。 What address stored in Usbs by malloc is overridden by Usbs = "/dev/ttyUSB1"; 通过malloc在Usbs存储的地址被Usbs = "/dev/ttyUSB1";覆盖Usbs = "/dev/ttyUSB1"; statement and Usbs has address to this constant string. 语句,并且Usbs具有此常量字符串的地址。
"/dev/ttyUSB1" is not dynamically allocated which you by mistake tried to free! "/dev/ttyUSB1"不是动态分配的,您错误地尝试释放它!

Do like this. 这样吧

char *DetectDevice(void)
    {   
        char *Usbs = malloc(1024);
        strcpy(Usbs,"/dev/ttyUSB1");
        return Usbs;
    } 

Usbs = "/dev/ttyUSB1"; changes Usbs to point to a string literal. Usbs更改为指向字符串文字。 This may exist in read-only memory and cannot be freed. 它可能存在于只读存储器中,无法释放。 Use 采用

char *DetectDevice(void)
{   
    char *Usbs = malloc(1024);
    strcpy(Usbs, "/dev/ttyUSB1");
    return Usbs;
}

to copy the string, or 复制字符串,或者

char *DetectDevice(void)
{   
    return strdup("/dev/ttyUSB1");
}

to allocate the string with just the right amount of memory instead. 分配恰好合适的内存量的字符串。

Alternatively, you could also recognise that DetectDevice returns a read-only string 另外,您还可以识别出DetectDevice返回一个只读字符串

const char *DetectDevice(void)
{   
    return "/dev/ttyUSB1";
}

and remove the free from calling code instead. 并从调用代码中删除free代码。

free comes with malloc or calloc or realloc It's always in that pair. freemalloccallocrealloc

free can be applied only on the pointers which are allocated using malloc or calloc or realloc . free只能应用于使用malloccallocrealloc分配的指针。

When you have allocated block of memory 1024 in function DetectDevice . 在功能DetectDevice分配了1024内存块后。 In the next line you have assigned the pointer to String literal "/dev/ttyUSB1" . 在下一行中,您已将指针分配给String文字"/dev/ttyUSB1"

So the same is returned and pointed by your pointer PathToDevice and now you are trying to free it, so you are getting this kind of error message. 因此,您的指针PathToDevice返回并指向了相同的PathToDevice ,现在您尝试释放它,因此,您将收到这种错误消息。

You should use strcpy(Usbs,"/dev/ttyUSB1") and then return Usbs . 您应该使用strcpy(Usbs,"/dev/ttyUSB1")然后返回Usbs

And You have not only problem with free but You are having memory leak also. 而且您不仅有free问题,而且还存在memory leak After removing free(PathToDevice) from your code you still have memory leak without any error. 从代码中删除free(PathToDevice) ,您仍然会发生内存泄漏,没有任何错误。

( Advice : It's not good practice to first allocate memory on heap and then code such that the pointer started pointing to other address. It will cause memory leak). 建议建议不要先在堆上分配内存,然后进行编码,使指针开始指向其他地址,这将是内存不好的做法)。

Although it may not be your problem here you should be checking that malloc actually returns a valid address and not NULL, especially on a system with limited resources such as the raspberry pi. 尽管这可能不是您的问题,但是您应该检查malloc实际返回有效地址而不是NULL,尤其是在诸如raspberry pi这样资源有限的系统上。

ie

char *DetectDevice(void)
    {   
        char *Usbs = malloc(1024);
        if(Usbs != NULL)
        {
           strcpy(Usbs,"/dev/ttyUSB1");
        }
        else
        {
          // malloc didn't allocate memory do something about it.
        }
        return Usbs;
    } 

You seem to expect this attribution Usbs = "/dev/ttyUSB1" means "write the content of the right-hand string into the memory pointed to by Usbs". 您似乎期望这个属性Usbs = "/dev/ttyUSB1"意思是“将右侧字符串的内容写入Usbs指向的内存中”。

This is not what it means. 这不是什么意思。 It means "overwrite Usbs to point to the location of the statically allocated constant string". 它的意思是“覆盖USB以指向静态分配的常量字符串的位置”。 Such a location is not managed by malloc and cannot be freed by free . 这样的位置不受malloc管理,不能由free释放。

To copy the contents of your constant string into Usbs you need to use the facilities in string.h . 要将常量字符串的内容复制到Usbs ,需要使用string.h

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

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