Reputation: 87
I have an error "Segmentation Fault SigSegv" in my C code:
int main(void)
{
HANDLE h;
char *query = malloc(10);
h=InitPort("\\\\.\\COM2",57600);
query = 0;
query=getenv("QUERY_STRING");
if (h==INVALID_HANDLE_VALUE)
{
printf("Error\n");
return 0;
}
if (strstr(query,"COMM=W")!=0)
{
SendData(h,'W');
}
return 0;
}
I read a lot of opinions about allocate memory and finally use malloc() function, but it didn't work.
All functions in my code:
HANDLE InitPort(char* PORT,unsigned long BAUD_RATE)
{
HANDLE h;
DCB d;
h=CreateFileA(PORT,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,0, 0);
GetCommState(h,&d);
d.BaudRate=BAUD_RATE;
d.fBinary=1;
d.fParity=0;
d.ByteSize=8;
d.StopBits=ONESTOPBIT;
SetCommState(h,&d);
return h;
}
void SendData(HANDLE h,unsigned char byte)
{
unsigned long n;
WriteFile(h,&byte,1,&n,NULL);
}
And *char query; query=(char)malloc(sizeof(char)10); didn't work too
Upvotes: 0
Views: 219
Reputation: 68
Well, you didnt add your other functions beside main, so I can't figer out the entire picture.
basicly, Segmantation fault casued by refrencing memory that already freed or using pointers in a wrong way.
The usage of malloc may be like this: char s; s=(char)malloc(sizeof(char)*size);
You don't really have to make the casting, but its not a problem.
Upvotes: 0