LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-28-2016, 09:49 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Inconsistent value returned by function


when I call the function default_ratio_check(...), I get confusing value.

Code:
const int COMP_READ_COMPRESS = 1;
const int COMP_WRITE_COMPRESS = 0;
const int COMP_TRACE_USE_DEFAULT_RATIO = 2;
double COMP_DEFAULT_RATIO = 0.20;

compress_struct* compress_global;

double default_ratio_check( char** s, int i)
{
    if( s == NULL ){
        if(COMP_TRACE_USE_DEFAULT_RATIO ) {
            return COMP_DEFAULT_RATIO;
        }
    }
    if( COMP_READ_COMPRESS || COMP_WRITE_COMPRESS ){
        if(COMP_TRACE_USE_DEFAULT_RATIO ) {
            COMP_DEFAULT_RATIO = atof( s[i] );
            return COMP_DEFAULT_RATIO;
        }
    }
    return 0;
}
I assign the result to the double type "ratio".

Code:
   ratio =  default_ratio_check( (char**)0, 0 );
the value assigned to ratio is 2.0, which is the value of COMP_TRACE_USE_DEFAULT_RATIO instead of COMP_DEFAULT_RATIO. So I change the value of COMP_TRACE_USE_DEFAULT_RATIO,correspondingly, the returned value to ratio is also followed. I have use gdb to step into the function, however , I find everything works correctly. But the return value is wrong after the default_ratio_check() returned.
Thanks for your helping. gcc version 5.2.1 20151010; ubuntu 15.10
 
Old 01-28-2016, 02:29 PM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
I think the fault is in how you are calling the function here
Code:
ratio =  default_ratio_check( (char**)0, 0 );
I assume you are trying to to pass the address of a "0" character ( as you convert that char to a float with atof ), you are actually passing a NULL pointer to the function, s will always = NULL in this case and so the first if statement is always true.

Try
Code:
char *buff1="1";
char *buff2="2";
double ratio;
ratio=  default_ratio_check(&buff1, 0 );
printf("result=%f\n",ratio);
ratio=  default_ratio_check(&buff2, 0 );
printf("result=%f\n",ratio);
Result is
Code:
result=1.000000
result=2.000000
That is if I understand what you are trying to do!
I asumne this is a fragment of a larger peice of code as there are easier ways of passing the args.
 
Old 01-28-2016, 02:53 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I agree with Keith Hedger.

In the debugger check what it's doing to (char **)0 and I think you'll find that it either pointing to a null, or bad location.

Instead create a character string such as:
Code:
static char zero_string[2] = "0\0";
And then pass the address of that.

I don't know why you need a pointer to a pointer either. A simple character pointer should do fine. You're not modifying the passed string after all.
 
  


Reply

Tags
gcc, linux, return



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User-specified malloc function returned NULL mahao Linux - Server 1 12-23-2010 10:41 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
C++ splice a list returned from a function lyle_s Programming 2 11-18-2009 02:04 PM
difference between socket returned descriptor and accept returned descriptor naveenisback Programming 1 08-29-2009 04:55 AM
Inconsistent permissions kosmonaft Linux - General 1 11-21-2005 11:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration