473,811 Members | 3,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(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
}
Feb 22 '06 #1
2 1971
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(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
So, 'mstmch' contains only 5 elements.
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 );
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<<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 )
{
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
4050
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 I am not fully convinced that this problem cannot be solved. I am going to propose a solution here. For the sake of discussion I will post my solution here. It is possible that the proposed solution does not work, feedback and comments are...
117
7278
by: Peter Olcott | last post by:
www.halting-problem.com
4
2408
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 hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
0
1601
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 can help me ... My environment: VS 2003 v7.1.3088, Win2K v5.0.2195 SP3, IE6 v6.0.2800.1106 browser. I have a class (C3Menu) derived from WebControl, with a property (MenuItems) that is a collection of menu items. The collection property is...
16
4932
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
2
2536
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 am really desperate now, because I havent' been able to locate the origin of the problem for a couple of days now.. PROBLEM: the message digest obtained differs each time I execute the code, but works perfectly when applying the "control", that...
5
13619
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 which seems to be an indeterminate or -infinity or something of the such. I am coding an IAC network and the problem occurs only in learning mode when the values approach 1.00 (the values SHOULD stay between between -0.2 and 1.0, but they...
18
3355
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 the image. The desired behavior is that when the pop-up is invoked, I want the underlying window to stay put. I don't have this problem when I run the code on my local computer but I do have it when I run the code on geocities.
7
4830
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 expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
12
2268
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 office. The original code's backend is using the SQL Server 2000 and I am testing to use it on the Express edition. And I run into the following problem.
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.