473,395 Members | 1,999 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 1946
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.