Runtime error with C program running Cygwin

Discussion in 'C Programming' started by nick048, May 29, 2007.

  1. nick048

    nick048 Guest

    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='\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='\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
     
    nick048, May 29, 2007
    #1
    1. Advertising

  2. On May 29, 1:57 am, nick048 <> 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.
     
    Quentin Godfroy, May 29, 2007
    #2
    1. Advertising

  3. nick048

    Thad Smith Guest

    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
     
    Thad Smith, May 29, 2007
    #3
  4. nick048 <> 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.
     
    Christopher Benson-Manica, May 29, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    470
  2. Replies:
    0
    Views:
    435
  3. WELCOME to

    , Sep 3, 2003, in forum: Python
    Replies:
    1
    Views:
    517
    Robin Becker
    Sep 5, 2003
  4. Replies:
    0
    Views:
    416
  5. Jorgen Grahn
    Replies:
    3
    Views:
    620
    Jorgen Grahn
    Feb 18, 2004
Loading...

Share This Page