Variable being set to 0 without appropriate instruction to do so

Over one line, a call to a function is changing a variable (Does not o

Page 1 of 1

2 Replies - 1068 Views - Last Post: 14 March 2008 - 04:21 AM Rate Topic: -----

#1 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Variable being set to 0 without appropriate instruction to do so

Posted 13 March 2008 - 07:26 PM

Hello all,

I am having a little problem. A variable holding an integer is changing during a function call that has nothing to do with the variable. The only thing that I could think of is perhaps the function is overwriting the memory location the variable is in?

Here is the relevant section of main():
		cin.getline(usr_input,INPUT_LENGTH);
		cout<<"\n1 1:"<<words_in_1<<"\n  2:"<<words_in_2<<"\n  3:"<<words_in_3;
		getWord(usr_input, word, char_marker, INPUT_LENGTH, WORD_LENGTH);
		cout<<"\n2 1:"<<words_in_1<<"\n  2:"<<words_in_2<<"\n  3:"<<words_in_3;



And here is the function getWord():
void getWord (char array[], char word[], int ch_place, int inputLength, int wordLength)
{
/*	* Takes input of the character array[] and places all characters from array[ch_place]
	* to the first space encountered/the end of array[]	*/

	int i; // Need i outside for loop
	/* From i=0, loop while we have not reached the end of users input, the ith array character is not
	 * a null or a space */
	
	clean(word,wordLength); 

	for ( i=0; (array[ch_place] != ' ') && (array[ch_place] != '\0') && (ch_place<inputLength); i++, ch_place++) 
	{
		word[i] = array[ch_place];
	}
	word[i] = '\0'; // Ensure the array word is terminated with a null character
	
}



And clean():
int clean(char array[], int array_length)
{
	for (int i=0; i <= array_length; i++)
	{
		array[i]='\0';
	}
	return 0;
}



And here is the output over the few lines of main():

Quote

1 1:0
2:0
3:3
2 1:0
2:0
3:0


This does not happen with words_in_1 nor words_in_2, and as far as I can see throughout the rest of my code words_in_* are always used in exactly the same way (just being for a different list #).

Any help, ideas or pointers would be greatly appreciated,
enpey

Is This A Good Question/Topic? 0
  • +

Replies To: Variable being set to 0 without appropriate instruction to do so

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 945
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Variable being set to 0 without appropriate instruction to do so

Posted 14 March 2008 - 02:45 AM

if array_length holds the length of your char array, then it would appear that you've got undefined behaviour here

	for (int i=0; i <= array_length; i++)
	{
		array[i]='\0';
	} 
if your array is of size array_length, then this will overflow to 1 past the last element.
Was This Post Helpful? 0
  • +
  • -

#3 enpey   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 75
  • Joined: 02-May 07

Re: Variable being set to 0 without appropriate instruction to do so

Posted 14 March 2008 - 04:21 AM

Thank you for that Bench, that has fixed it up.

Silly me...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1