473,395 Members | 1,595 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with return

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[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
}
Feb 22 '06 #1
2 1945
cdg wrote:
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[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
You cannot "test only" any code _after_ a 'return' statement in
your function.
}


V
--
Please remove capital As from my address when replying by mail
Feb 22 '06 #2
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.
Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
117
by: Peter Olcott | last post by:
www.halting-problem.com
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
5
by: Jean-François Michaud | last post by:
Hello people, I am rather puzzled by this problem I am having with doubles here. The code seems to execute correctly for awhile and then all a sudden printfing out my doubles, I get -1.#IND00...
18
by: len.hartley | last post by:
Hi, I am trying to pop-up a window when the user clicks on an image. The problem is that when the user clicks on the image and the window pops up OK, but the window underneath also proceeds to...
7
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.