Thread: if vs else if. Seems like a huge difference

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    5

    if vs else if. Seems like a huge difference

    I'm writing a client-server application. Attached is its simplified version which narrows down my issue. The idea is, that server sends the sturcture of 3 ints. Client receives the structure and based on the values, performs certain actions. This is performed in a loop. Here's a fragment from test_client.c:
    Code:
    numRead = fread(&array, sizeof(struct three_ints), 1, sock_fp);
    if (numRead <=0)
    	printf("numRead is now &#37;d\n", numRead);
    sleep(2);
    
    if (array.one == 0 && array.two == 0 && array.three == 0)
    {
    printf("Read zeros. Their current values are %d, %d, %d\n", array.one, array.two, array.three);
    printf("About to read integers and char. Their current values are %d, %d, %c\n", other_array.intOne, other_array.intTwo, other_array.charOne);
    
    fread(&other_array, sizeof(struct two_ints_char), 1, sock_fp);
    
    printf("Read the integers and char. Their current values are %d, %d, %c\n\n", other_array.intOne, other_array.intTwo, other_array.charOne);
    sleep(4);
    }
    
    else if ( array.one == -5 && array.two == -5 && array.three == -5 )
    {
                  printf("I am at -5\n");
    }
    
    else if ( array.one == -10 && array.two == -10 && array.three == -10 )   
    {
                 printf("I am at -10\n");
    }
    
    else
    {
    printf("Read non-zeros. Their current values are %d, %d, %d\n", array.one, array.two, array.three);
    printf("About to send some_int to server %d\n", ++some_int);
    fwrite(&some_int, sizeof(int), 1, sock_fp);
    printf("some_int sent\n\n");
    sleep(6);
    }
    This version works fine. However, once I replace "else if" with just "if" (until recent I believed the outcomes are the same), the following function fails:

    numRead = fread(&array, sizeof(struct three_ints), 1, sock_fp);

    and numread contains zero value. It took me 2 days to figure it out, but why does it happen and how can "if" statement affect fread function I still have no idea. Can someone explain???

    Thanks

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The ONLY way this could be happening is if this line:

    Code:
    fread(&other_array, sizeof(struct two_ints_char), 1, sock_fp);
    Is overwriting the other array.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A couple of things spring to mind.
    1. You don't open the streams in binary mode.
    2. You're making a lot of assumptions about the size, padding, alignment and endian-ess of those structs as they're represented on the client and the server.

    Consider using wireshark / ethereal to trace the messages on the wire.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    The ONLY way this could be happening is if this line:

    Code:
    fread(&other_array, sizeof(struct two_ints_char), 1, sock_fp);
    Is overwriting the other array.
    I didn't change the logic - just replaced "if" with "else if" that's all. And it's not happening anymore. And the line is overwriting the values of other_array structure but I don't think there's anything wrong with that

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mityay View Post
    I didn't change the logic - just replaced "if" with "else if" that's all. And it's not happening anymore. And the line is overwriting the values of other_array structure but I don't think there's anything wrong with that
    If it's not happening anymore, then I suspect the problem never had anything to do with this piece of code. There's nothing wrong with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 06:55 AM