/* 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
by the way… this program is compiled on a raspberry pi!
Usbs = "/dev/ttyUSB1";changesUsbsto point to a string literal. This may exist in read-only memory and cannot be freed. Useto copy the string, or
to allocate the string with just the right amount of memory instead.
Alternatively, you could also recognise that
DetectDevicereturns a read-only stringand remove the
freefrom calling code instead.