Thread: Need help returning -1 in function

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Need help returning -1 in function

    Hi all, I should have signed up here about a year ago, as I could use help from time to time.

    My current issue is that the following function if 'NUL', 'ETK' or 'NAK' are found in a string (containing up to 1500 characters), the function is to return -1 and a signal[] string is to have "NOT-VALID."

    The issue I am having is the return value not being -1 but 4294967295 and the signal string even though it appears to be correct isnt' (I think i may have to do with '\0'.

    I've tried casting, signed long, etc but nothing seems to work and I don't understand why.

    Yes I know the code is crappy, but I'm still a student. I'd appreciate any help I can get.
    Thanks.

    Code:
    long analyze(const char rawData[ ], char signal[ ], int frequency, int a, int b)
    {
    
        long rv = 0,i = 0,st = 0;
        int found = 0;
        int invalid = 0;
    
        while(rawData[i] != '\0' && invalid != 1)
        {
    
            if (rawData[i] >= frequency + 48 && rawData[i] <= 57)
            {
                if (rawData[i+61] == rawData[i])
                {
                    if (rawData[i+122] == rawData[i])
                    {
                        found = 1;
                        i += 123;
                    }
                }
            }
    
            if (found)
            {
                switch(rawData[i])
                {
                case 'E':
                    if (rawData[i+1] == 'T' && rawData[i+2] == 'X')
                    {
                        rv = -1;
                        strcpy(signal, "NON-SEQUITOR");
                        invalid = 1;
                    }
                    break;
                case 'N':
                    if (rawData[i+1] == 'U' && rawData[i+2] == 'L')
                    {
                        rv = -1;
                        strcpy(signal, "NON-SEQUITOR");
                        invalid = 1;
                    }
                    break;
                case 'A':
                    if (rawData[i-1] == 'N' && rawData[i+2] == 'K')
                    {
                        rv = -1;
                        strcpy(signal, "NON-SEQUITOR");
                        invalid = 1;
                    }
                    break;
                case '<':
                    if (rawData[i+1] == '<')
                        signal[st] = rawData[i + 2] - frequency;
                    break;
                case '>' :
                    if(rawData[i + 1] == '>')
                        signal[st] += rawData[i + 2] + frequency;
                    break;
                case ' ':
                    signal[st] = ' ';
                    st++;
                    while(rawData[i + 1] == ' ')
                        i++;
                    break;
                case '\0':
                    signal[st] = '\0';
                    break;
                default:
                    if(rawData[i] >= a && rawData[i] <= b)
                    {
                        signal[st] = rawData[i];
                        st++;
                        rv = strlen(signal);
                     }
                    break;
                }
            }
            i++;
        }
        return rv;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, not to say that the function works, but the problem wouldn't be here since the function returns signed long. What could be happening is that the type changes when you use the return value. If in a larger expression the type becomes unsigned, that would explain things.

    Code:
    #include <stdio.h>
    
    long foo(void) {
       return -1;
    }
    
    int main(void) {
       printf("return value = %lu\n", foo());
       return 0;
    }
    C code - 10 lines - codepad

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You should also provide a minimal main() function which calls your analyze function with some input of your choice. This is the way to test whether your function behaves as intended.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Alright I fixed the return -1 issue, but I can't seem to figure out why signal[] is printing out a blank when using my profs main.

    Its not making sense to me at all. When result = -1, NON-SEQUITOR should be in signal[], but the test is returning ' '.

    Any ideas?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by compile_ View Post
    Alright I fixed the return -1 issue, but I can't seem to figure out why signal[] is printing out a blank when using my profs main.

    Its not making sense to me at all. When result = -1, NON-SEQUITOR should be in signal[], but the test is returning ' '.

    Any ideas?
    Quote Originally Posted by c99tutorial View Post
    You should also provide a minimal main() function which calls your analyze function with some input of your choice. This is the way to test whether your function behaves as intended.
    Please.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Quote Originally Posted by whiteflags View Post
    Please.



    Code:
    #include <string.h>
    #include <stdio.h>
    long analyze(const char rawData[ ], char signal[ ], int frequency, int a, int b);
    int main ()
    {
        int frequency=0;
        signed long result;
        int a=0, b=0;
        char signal[121], rawData[ ] =
          ""; /* NO SIGNAL (empty string) f=0,a=0,b=0 */;
        result = analyze(rawData, signal, frequency, a, b);
        printf("Result = %lu\n", result);
        printf("Signal = %s", signal);
    
    
        return 0;
    }
    There you go.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    OK, if that is really the data you're worried about then it's pretty obvious.

    >> while( rawData[i] != '\0' && ... )
    This is false. Examining the contents of rawData[0] gives '\0'. So you skip the whole loop.

    >> if( found )
    Found is 0 so you skip this.

    >> return rv;
    rv is 0 so you end up with that as the return value. The content of signal in a release build is actually undefined (read: garbage). In a debug build the string is probably initialized for you because that helps you prove the program's correctness.

    If you want to catch an error in this situation, some of the logic in the function could be modified. For example, check if rawData[0] == '\0' before you start processing it. Exit early after setting up your error.

    >> printf("Result = %lu\n", result);
    %ld would show -1, because long is signed; the above code was only to show you the error and not something you should copy.
    Last edited by whiteflags; 02-02-2013 at 04:52 PM.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    the char signal[] = " ";
    I can't figure out a way of setting signal[] to "NON-SEQUITOR" when the string is only that big. I think it has to do with the Case ' ': but i'm unsure.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I just got done telling you that you showed me a special case for input to the function. I don't know why you don't believe me.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
        while(rawData[i] != '\0' && invalid != 1)
        {
     
            if (rawData[i] >= frequency + 48 && rawData[i] <= 57)
            {
                if (rawData[i+61] == rawData[i])
                {
                    if (rawData[i+122] == rawData[i])
    These are just buffer overflows, unless your "string" has some 'magical' properties like lots of embedded \0 characters.

    Code:
                case '<':
                    if (rawData[i+1] == '<')
                        signal[st] = rawData[i + 2] - frequency;
                    break;
                case '>' :
                    if(rawData[i + 1] == '>')
                        signal[st] += rawData[i + 2] + frequency;
                    break;
                case ' ':
                    signal[st] = ' ';
                    st++;
                    while(rawData[i + 1] == ' ')
                        i++;
                    break;
                case '\0':
                    signal[st] = '\0';
                    break;
                default:
                    if(rawData[i] >= a && rawData[i] <= b)
                    {
                        signal[st] = rawData[i];
                        st++;
                        rv = strlen(signal);
                     }
                    break;
    The first two cases don't increment st, so any saved character is just going to get overwritten by other data.
    No case except the \0 case puts a \0 in the correct place in the signal string.
    The default case calls strlen(), which WILL blow up if you're at all sloppy (which you are) with placing \0's in your signal string as you create it.

    EVERY case needs to make sure a \0 exists in the correct place at every step, if you're calling any other string functions within the switch.
    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. Function returning function pointer : help
    By ypp174 in forum C Programming
    Replies: 5
    Last Post: 09-17-2012, 01:10 AM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Returning a function pointer from a function
    By StainedBlue in forum C++ Programming
    Replies: 4
    Last Post: 11-01-2010, 10:26 PM
  4. Error: _ defined as a function returning a function?
    By Jardon in forum C Programming
    Replies: 15
    Last Post: 07-29-2009, 11:53 AM
  5. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM