The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-28-2009
dpmore dpmore is offline
Registered User
  
 

Join Date: May 2008
Posts: 3
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
  #2 (permalink)  
Old 09-28-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,807
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0