![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sqlplus returning value - remove carriage return '\r' - Please help | sunshine1974 | Shell Programming and Scripting | 10 | 09-18-2009 09:19 PM |
| Returning the name of function used | kingpin2502 | Shell Programming and Scripting | 3 | 06-15-2009 06:55 PM |
| [Bash]Function returning a boolean | dolphin06 | Shell Programming and Scripting | 6 | 04-29-2009 12:31 PM |
| returning from a function | alirezan | Shell Programming and Scripting | 2 | 08-18-2008 04:12 PM |
| string returning function | jisc | High Level Programming | 5 | 03-23-2006 10:35 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Function Returning Value w/o return stmt
I am working on a C/Unix application from last 2 years which communicates with other systems using proprietary format of my client. We have a function written in C which returns integer, which is response from other system to the request message initiated by my system. This return value is then assigned to a variable which determines if the transmission was successful or failed. On failure my application is suppose to show some error message to user.
This system has been working proper from long time. We recently got one production issue where application doesn’t display error message to user even when communication returns non-zero (Failure!) return code. While analyzing we found that the return statement itself was missing from long time. Now we wonder how the function was returning the correct value working earlier in production w/o return statement. When we tried to find if there is any change is environment and the only change we could find all the servers who reported issue was recently upgraded to RED HAT Linux 4.0. Now I really don’t know how to explain this to client since though this is a fact but I need to have some technical explanation behind this. Following is the dummy code snippet to explain scenario, void main () { int responce_code=0; responce_code=ParseResponse(); if (responce_code != 0) { Show_Error(); } } int ParseResponse() { // We have some parser code to extract the response log_message("response from other system, %s",respose) atoi(respose); } Now in above statement atoi(respose); should actually be return atoi(respose); in order to get the return code to main(). But still it used to work fine and all of sudden stared failing after upgrade to Red Hat Linux 4 . It would be great help if some one can explain how this can happen in C for the Linux OS version below RED HAT 4 Thanks in Advance, Deepak |
|
||||
|
First off, compile with gcc -Wall then it won't happen again if you pay attention to warnings. A good compile is zero warnings and zero errors, nothing else is acceptable. When a function returns an int and nothing is returned by the function - then - whatever garbage data was living on the stack is what gets returned. ie., any value is possible. If your code looks like Code:
int retval=0; retval=myfunc(); if(retval) // good return else // zero == error return Then any garbage on the stack will result in a "good" return. This is called programming by coincidence, in polite circles, when it actually works. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|