problem with return

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cdg

    #1

    problem with return

    Could anyone tell me why a function return with this program is not
    returning to "main". The program will compile and should be
    self-explanatory. And any suggestions for improvements would be appreciated
    also.
    The function is HashMilliTime at the end of the program.

    #include <sys/timeb.h>
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>

    unsigned short GetMilliTime(un signed short *); //function prototype
    unsigned HashMilliTime (char[], int); //function prototype

    void main()
    {
    unsigned short mltm(0); //milli-seconds for time
    char mltmch[5] = {0}; //milli-seconds as character array
    int mltmln(0); //length of milli-seconds as character array
    // unsigned h(0); //hashed milli-seconds

    GetMilliTime(&m ltm);

    cout<<mltm<<end l; //test only remove later

    itoa( mltm, mltmch, 10 );

    cout<<mltmch<<" char"<<endl; //test only remove later

    mltmln = strlen(mltmch);

    cout<<mltmln<<e ndl; //test only remove later

    cout<<mltmch<<" before"<<endl; //test only remove later

    HashMilliTime(m ltmch, mltmln);

    //**The returned unsigned int will not print here**
    cout<<mltmch<<" should be same as h"<<endl; //test only remove later

    cout<<mltmln<<e ndl; //test only remove later

    }

    unsigned short GetMilliTime(un signed short *mltm)
    {
    struct _timeb tstruct;
    _ftime( &tstruct );
    *mltm = tstruct.millitm ;
    return *mltm;
    }

    unsigned HashMilliTime (char key[], int len )
    {
    char *p = key;
    unsigned h = 0;
    int i(0);
    unsigned x(0);

    for ( i = 0; i < len; i++ )
    {
    h += p[i];
    h += ( h << 10 );
    h ^= ( h >> 6 );
    }

    h += ( h << 3 );
    h ^= ( h >> 11 );
    h += ( h << 15 );

    cout<<len<<" hash"<<endl; //test only remove later
    cout<<h<<" hash"<<endl; //test only remove later

    return h;
    x = len + 5; //test only remove later
    return x; //test only remove later
    }


  • Victor Bazarov

    #2
    Re: problem with return

    cdg wrote:[color=blue]
    > Could anyone tell me why a function return with this program is not
    > returning to "main".[/color]

    The program has undefined behaviour. You're most likely stepping all over
    the stack by overrunning the bounds of a local array.
    [color=blue]
    > The program will compile[/color]

    No, it won't. At least on my system.
    [color=blue]
    > and should be
    > self-explanatory. And any suggestions for improvements would be appreciated
    > also.[/color]

    First and foremost, your program contains non-standard constructs, like
    '<sys/timeb.h>', '<iostream.h>' , "void main", 'itoa', '_timeb', '_ftime'.
    The behaviour of the program with all those is _undefined_ in Standard
    C++ terms. Making certain concessions, I can probably tell you what
    _else_ is wrong with it. See below.
    [color=blue]
    > The function is HashMilliTime at the end of the program.
    >
    > #include <sys/timeb.h>
    > #include <iostream.h>
    > #include <string.h>
    > #include <stdlib.h>
    >
    > unsigned short GetMilliTime(un signed short *); //function prototype
    > unsigned HashMilliTime (char[], int); //function prototype
    >
    > void main()
    > {
    > unsigned short mltm(0); //milli-seconds for time
    > char mltmch[5] = {0}; //milli-seconds as character array[/color]

    So, 'mstmch' contains only 5 elements.
    [color=blue]
    > int mltmln(0); //length of milli-seconds as character array
    > // unsigned h(0); //hashed milli-seconds
    >
    > GetMilliTime(&m ltm);
    >
    > cout<<mltm<<end l; //test only remove later
    >
    > itoa( mltm, mltmch, 10 );[/color]

    Are you sure that converting 'mltm' here does not step _over_ the
    boundary of 'mltmch' array? Remember, it only has 5 elements, thus
    leaving only 4 characters in the C-string.
    [color=blue]
    >
    > cout<<mltmch<<" char"<<endl; //test only remove later
    >
    > mltmln = strlen(mltmch);
    >
    > cout<<mltmln<<e ndl; //test only remove later
    >
    > cout<<mltmch<<" before"<<endl; //test only remove later
    >
    > HashMilliTime(m ltmch, mltmln);
    >
    > //**The returned unsigned int will not print here**
    > cout<<mltmch<<" should be same as h"<<endl; //test only remove later
    >
    > cout<<mltmln<<e ndl; //test only remove later
    >
    > }
    >
    > unsigned short GetMilliTime(un signed short *mltm)
    > {
    > struct _timeb tstruct;
    > _ftime( &tstruct );
    > *mltm = tstruct.millitm ;
    > return *mltm;
    > }
    >
    > unsigned HashMilliTime (char key[], int len )
    > {[/color]

    Add

    assert(len < 5);

    here.
    [color=blue]
    > char *p = key;
    > unsigned h = 0;
    > int i(0);
    > unsigned x(0);
    >
    > for ( i = 0; i < len; i++ )
    > {
    > h += p[i];
    > h += ( h << 10 );
    > h ^= ( h >> 6 );
    > }
    >
    > h += ( h << 3 );
    > h ^= ( h >> 11 );
    > h += ( h << 15 );
    >
    > cout<<len<<" hash"<<endl; //test only remove later
    > cout<<h<<" hash"<<endl; //test only remove later
    >
    > return h;
    > x = len + 5; //test only remove later
    > return x; //test only remove later[/color]

    You cannot "test only" any code _after_ a 'return' statement in
    your function.
    [color=blue]
    > }
    >
    >[/color]

    V
    --
    Please remove capital As from my address when replying by mail

    Comment

    • cdg

      #3
      Re: problem with return

      Thanks for your post.

      And I am not very far in along in what I have learned about C++, and it is
      all self-taught. But your post did help me to understand about "defined
      behaviour". And what is needed to write up-to-date programs.
      Also, I figured out why the return value wasn`t being returned. I was
      writing it incompletely, since the function call needed a Lvalue. This was
      the problem, not the array size. Since milli-seconds would never go over
      1000, or possibly 999, if milli-seconds starts at zero.
      And the second return was an obvious mistake, too.


      Comment

      Working...