Thread: side effect of calling a function without specifying anywhere for the return value

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    2

    side effect of calling a function without specifying anywhere for the return value

    Hi,
    Sorry about the title.
    I've just come across some code that I wrote a long time ago. I know the code is wrong and needs fixing, but I'm trying to ascertain if it could be the cause of my problem... as follows..

    What will happen at runtime with the following snippet...

    Code:
    int fnc(int arg1) 
    {
       int retval;
    
       ....
       return somevalue;
    }
    
    fnc(1);
    (i.e not calling fnc with a variable or a cast).

    I am asking as, after calling this snippet a few million times, I am seeing strange behavior - seemingly at random - appearing in the program.
    The code has been running on various computers for over 10 years, but we are just seeing the behavior recently.
    It's hard to explain, but seems to start with variable (static data) corruption.

    Many Thanks,
    Ian

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What else is going on in the function?
    The return variable pops out of nowhere.

    Ignoring the return result is easy and ubiquitous in C. Who looks at the result of printf or strcat.
    Sure you could void cast it, but that won't change what the function does.

    Did you change or update your compiler / OS / anything else to provoke this sudden stoppage?
    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.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    2
    The function is quite complicated - and I'm clutching at straws trying to figure whats going wrong (these types of problems are always a challenge in legacy code).

    The compiler hasnt changed, but it runs on many types of o/s and servers. It's just this one big one that is exhibiting the problem.

    I was wondering if the code above would undefined behavior - which is usually instrumental in causing weirdness.

    Ian.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    It could be something we can't see without the whole code. Or a small program that replicates the behavior, if you can.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The function is quite complicated
    And you wanted us to extrapolate your problem from just 3 lines of code?

    Myriad are the ways it could be messed up.
    I'm guessing that this is proprietary code, so there's no way for you to post it.

    > It's hard to explain, but seems to start with variable (static data) corruption.
    Start with nearby static arrays, and look for overruns.

    Can you refactor
    Code:
    int fnc ( int arg ) {
      // whole mess of code
      return result;
    }
    Into
    Code:
    int fnc_part1 ( ) {
      // half a mess of code
    }
    int fnc_part2( ) {
      // the other half a mess of code
    }
    int fnc ( int arg ) {
      fnc_part1();
      return fnc_part2();
    }
    You might be able to figure out which part is the problem.

    You didn't even tell us which compiler.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. how to return 2 values upon calling a function
    By David_Baratheon in forum C Programming
    Replies: 1
    Last Post: 05-10-2012, 10:05 AM
  3. Replies: 16
    Last Post: 11-18-2011, 12:13 AM
  4. Replies: 2
    Last Post: 11-13-2009, 05:33 AM
  5. Side effect of various operations
    By xds4lx in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 10:12 AM

Tags for this Thread