|
|
 |
 |
 |
 |
Runtime error with C program running Cygwin
Hi to all, I have written a little Server program in C and I have compiled this with Cygwin without errors. This is my code: // Prototype void wordReceived(int sd,char *s); int main (unsigned argc, char **argv) { int SERVER_PORT, sock, client_len, fd; struct sockaddr_in server, client; char *theword; . . <OTHER CODE> . wordReceived(fd,theword); /* Process the word received from client*/ . . <OTHER CODE> . }
void wordReceived(int sd,char *s){ int i=0; int charRecv = 0; char c[1]; charRecv = recv(sd, c, 1, 0); while (c[1] !='\n'){ s[i++]=c[1]; charRecv = recv(sd, c, 1, 0); } // End while (c !='\n') s[i]='\0'; }
When the program call wordReceived, Cygwin return to me this message: "5 [main] myserver 2736 _cygtls:: handle_exception: Error while dumping state (probably corrupted stack)" and the program hang. I have modified my program in this way: int main (unsigned argc, char **argv) { int SERVER_PORT, sock, client_len, fd; struct sockaddr_in server, client; char theword[30]; char c; int i, charRecv; . . <OTHER CODE> . i=0; charRecv = recv(fd, &c, 1, 0); while (c !='\n') { theword[i++]=c; charRecv = recv(fd, &c, 1, 0); } // End while (c !='\n') theword[i]='\0'; . . <OTHER CODE> . }
In this way the program run fine. Wich is my error in the first program ? How can modify my code in order to use correctly wordReceived? I hope in Your help in order to resolve my little problem Thank You and Best Regards Nick
On May 29, 1:57 am, nick048 <nicosia.gaet@moonsoft.it> wrote: > Hi to all, > I have written a little Server program in C and I have compiled this > with Cygwin without errors. This is my code: > int main (unsigned argc, char **argv) { > int SERVER_PORT, sock, client_len, fd; > struct sockaddr_in server, client; > char *theword;
You declare a char pointer uninitialized > <OTHER CODE>
Do you do something to this pointer in this code? > wordReceived(fd,theword); /* Process the word received from > client*/
You call wordRecieved with theword (as far as I can see, uninitialized. That is,you give a random address to your function). > void wordReceived(int sd,char *s){ > int i=0; > int charRecv = 0; > char c[1]; > charRecv = recv(sd, c, 1, 0); > while (c[1] !='\n'){ > s[i++]=c[1];
And then you write to this random address, where the program has probably no right to write. > When the program call wordReceived, Cygwin return to me this message: > "5 [main] myserver 2736 _cygtls:: handle_exception: Error while > dumping state (probably corrupted stack)" > and the program hang.
I don't know cygwin, but I bet that this error means a segmentation violation. Cheers, Quentin.
nick048 wrote: > charRecv = recv(sd, c, 1, 0); > When the program call wordReceived, Cygwin return to me this message: > "5 [main] myserver 2736 _cygtls:: handle_exception: Error while > dumping state (probably corrupted stack)" > and the program hang. > charRecv = recv(fd, &c, 1, 0); > In this way the program run fine.
What is the prototype for recv and description of parameters? -- Thad
nick048 <nicosia.gaet @moonsoft.it> wrote: > I have written a little Server program in C and I have compiled this > with Cygwin without errors. I was going to say "turn up the warning level on your compiler", but I found to my dismay that gcc will accept your original code silently; perhaps I have merely been coding in Java for too long. > char theword[30]; > charRecv = recv(fd, &c, 1, 0); > while (c !='\n') > { > theword[i++]=c; > charRecv = recv(fd, &c, 1, 0); > } // End while (c !='\n') > In this way the program run fine.
Note that it only "runs fine" if you receive 29 characters or less. If you can't arrange to know how many characters you are going to receive, you will want to use malloc() and realloc(). > Wich is my error in the first program ? > How can modify my code in order to use correctly wordReceived? > I hope in Your help in order to resolve my little problem > Thank You and Best Regards > Nick
-- C. Benson Manica | I *should* know what I'm talking about - if I cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
|
 |
 |
 |
 |
|