LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-14-2006, 06:42 AM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Problem with sending a signed int to another signed int. Almost random number given.


The following code is filled with holes (not that it would not make it run, it runs but with a strange problem), because it is a snippet from a bigger block of code of another program. I am doing this to clean up code. My aim in the following section of code is check to see how many elements are in a number within the vector ivar, then combine all of them into one number. So if ivar[0] = 2 and ivar[1] = 4, then the code would combine them and it would be 24. I was using math.h to do exponent operations, but it required changing several variables to doubles, and it would have made bigger problems as the code grew. So just to simplify things I made the function powerfunc. Powerfunc seems to work fine, its the loop in main that does not seem to. Loop outpus correct values but the values that are being added together gives out almost random numbers. Here is that output followed by some more details:

Code:
1
10
100
1000
10000
100000
100000
itemp_store += 1 * 100000 which equals 100000
itemp_store equals -1209866232
itemp_store += 5 * 10000 which equals 50000
itemp_store equals -1209816232
itemp_store += 2 * 1000 which equals 2000
itemp_store equals -1209814232
itemp_store += 4 * 100 which equals 400
itemp_store equals -1209813832
itemp_store += 2 * 10 which equals 20
itemp_store equals -1209813812
itemp_store += 8 * 1 which equals 8
itemp_store equals -1209813804
As you can immediatly see, everything goes fine until itemp_store is asked for its value, and it comes out wacked out. Though, if you notice, it is actualy adding the values I wanted into that negative number. So it is almost doing what it needs to do, but I don't know where it is getting that base negative number to begin with. That is basicly my question... I'll add a few comments in the code just to speed things(clear) things up if needed, but I won't swamp it. Thank you in advance!

Code:
#include <iostream>
#include <vector>
using namespace std;

// Every iteration of cout is there to just see what the value or output of something is.
int powerfunc(int x, int y)
{
        // power is started at one, and is multiplied by ten everything. 1, 10, 100, 1000, 10000...
	int itemp;
	for ( int i = 0, power = 1; i <= y; i++, power = (power*x) )
	{
                // itemp doesn't really need to be changed to a different value every loop, but it
                // doesn't kill anything.
		itemp = power;
		cout << itemp << endl;
	}
	cout << itemp << endl;
	return itemp;
}

int main()
{
        // Vector was needed in the code this code came from.
	vector<int> ivar(6);
	int itemp_store;
	
	ivar[0] = 1;
	ivar[1] = 5;
	ivar[2] = 2;
	ivar[3] = 4;
	ivar[4] = 2;
	ivar[5] = 8;
	
	for ( int a = 0; a < ivar.size(); a++ )
	{
		if ( a == ivar.size()-1 )
		{
                        // power goes backwards so it is sychronized when converting ivar's current
                        // element to it's proper integer location.
			for ( int b = 0, power = powerfunc(10,a); b <= a; b++, power = (power/10) )
			{
				itemp_store += ivar[b] * power;
				cout << "itemp_store += " << ivar[b] << " * " << power << " which equals " << ivar[b]*power << endl;
				cout << "itemp_store equals " << itemp_store << endl;
			}
			break;
		}
	}	
	return 0;
}
PS: If you think (or know) any part of my code is outright retarded, please give me a comment why it is a waste or etc, so I can learn better coding techniques. Thanks in advance!
 
Old 08-14-2006, 06:50 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
You never initialized itemp_store:
Code:
int itemp_store = 0;
EDIT:
Also your complete outer for-loop (for (int a=0...)) is needles, because you only run this loop until a==ivar.size()-1 and do nothing else, so why not simply set a=ivar.size()-1 or maybe even better let the "b-loop" run as long as b<ivar.size():
Code:
for ( int b = 0, power = powerfunc(10,a); b < ivar.size(); b++, power = (power/10) ){
  itemp_store += ivar[b] * power;
  cout << "itemp_store += " << ivar[b] << " * " << power << " which equals " << ivar[b]*power << endl;
  cout << "itemp_store equals " << itemp_store << endl;
}

Last edited by Flesym; 08-14-2006 at 07:10 AM.
 
Old 08-14-2006, 06:35 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Thank you it works, and yes that loop works too, I'll use it. BTW why do I have to initialize it? The following works, which is more or less what I tried to do:

Code:
	int i;
	i += 4;
	
	cout << i << endl;
It outputs 4 on my screen. Or is just not good practice to do what I did, for the error I found?
 
Old 08-14-2006, 08:22 PM   #4
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> The following works

no it does not work, you are just getting lucky.

> Or is just not good practice to do what I did, for the error I found?

it is not good practice to use uninitialized variables, because there is absolutely no gurantee as to what they will hold initially
 
Old 08-15-2006, 07:14 AM   #5
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
xhi I know your intelligent when it comes to programming, so the following question does not try to undermine you. I'm not sure what you mean by getting lucky. It works or doesn't. Why does my last piece of code work?
 
Old 08-15-2006, 07:33 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
You were lucky that 'i' was initialized to 0 (and not to another value). Not sure, but I have the feeling that some languages do default initializations of variables and others don't.
 
Old 08-15-2006, 10:52 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Wim is correct.

in c and c++ the declaration
Code:
int x;
does not gurantee the initial value of x. x is just as likely to have the value 0 as it is to have the value 39393. youll typically hear the word "garbage" used to describe memory, and this is exactly what is in a variable initially, garbage. even if it ends up being the right value (zero in your case) it is still garbage, just lucky garbage.

so if any operation other than assignment happens to a varible first, then you should initialize the variable to 0 (or an otherwise suitable value)..

hth
 
Old 08-15-2006, 12:35 PM   #8
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,425

Rep: Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159Reputation: 1159
All variables should always be expressly initialized to known values.
 
Old 08-15-2006, 12:38 PM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by sundialsvcs
All variables should always be expressly initialized to known values.
unless the variable is guranteed to have an assignment performed on it before any other operations. then it is acceptable to leave it as
Code:
int x;
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is a signed rpm? amlucent23 Fedora 1 06-24-2006 01:49 PM
signed and unsigned ArthurHuang Programming 4 05-23-2006 04:46 AM
Finally signed up! nightowl0v0 LinuxQuestions.org Member Intro 1 01-13-2006 12:36 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 10:34 PM
signed .... linuxanswer Programming 3 11-28-2003 10:20 PM


All times are GMT -5. The time now is 11:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration