problem with return

C

cdg

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(unsigned 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(&mltm);

cout<<mltm<<endl; //test only remove later

itoa( mltm, mltmch, 10 );

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

mltmln = strlen(mltmch);

cout<<mltmln<<endl; //test only remove later

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

HashMilliTime(mltmch, mltmln);

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

cout<<mltmln<<endl; //test only remove later

}

unsigned short GetMilliTime(unsigned 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;
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
}
 
V

Victor Bazarov

cdg said:
Could anyone tell me why a function return with this program is not
returning to "main".

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

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

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.
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(unsigned 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

So, 'mstmch' contains only 5 elements.
int mltmln(0); //length of milli-seconds as character array
// unsigned h(0); //hashed milli-seconds

GetMilliTime(&mltm);

cout<<mltm<<endl; //test only remove later

itoa( mltm, mltmch, 10 );

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.
cout<<mltmch<<" char"<<endl; //test only remove later

mltmln = strlen(mltmch);

cout<<mltmln<<endl; //test only remove later

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

HashMilliTime(mltmch, mltmln);

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

cout<<mltmln<<endl; //test only remove later

}

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

unsigned HashMilliTime (char key[], int len )
{

Add

assert(len < 5);

here.
char *p = key;
unsigned h = 0;
int i(0);
unsigned x(0);

for ( i = 0; i < len; i++ )
{
h += p;
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


You cannot "test only" any code _after_ a 'return' statement in
your function.

V
 
C

cdg

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top