3 Replies - 210 Views - Last Post: 29 September 2019 - 06:25 AM Rate Topic: -----

#1 ghostname0x00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-September 19

Simple function, weird output

Posted 29 September 2019 - 05:26 AM

I was trying to make a function returning biggest nr in array, something was off so I've added more output to find the problem, and it just amaze me:

#include <iostream>


//INIT ARR OF NRS AND RESULT VAR:
int numbers[32], ret;
//RETURN BIGGEST INT:
void biggest() {
	std::cout << "\nInitial SIZEOF numbers is: " << sizeof(numbers) << std::endl;
	std::cout << "\nGive me integers separated by spaces...:\n"; std::cin >> *numbers;
	std::cout << "\nMemory Address of numbers is: \n"<< numbers << std::endl;
	std::cout << "\nInserted SIZEOF numbers is: " << sizeof(numbers) << std::endl;

	for (int i = 0; i < sizeof(numbers); i++) {
		std::cout << numbers[i] << '\n';
		/*if (ret < numbers[i]) {
			ret = numbers[i];
		};*/
	}
	std::cout << "The biggest in numbers is :\n" << ret;

}

int main()
{
	biggest();
}



output:

Quote

Initial SIZEOF numbers is: 128

Give me integers separated by spaces...:
1 2 3

Memory Address of numbers is:
00007FF7A1F6D720
'nInserted SIZEOF numbers is: 128
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
256
0
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
The biggest in numbers is :
0
TaskOne.exe (process 512) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Im just learning C++ and I have no idea what is going on here. Why it shows me things like '-1' or 256 instead of inserted values? I just wanted to print out all elements of this array to operate on it.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple function, weird output

#2 jimblumberg   User is offline

  • member icon

Reputation: 5771
  • View blog
  • Posts: 17,672
  • Joined: 25-December 09

Re: Simple function, weird output

Posted 29 September 2019 - 06:21 AM

Quote

Why it shows me things like '-1' or 256 instead of inserted values?

Probably because you're accessing the array out of bounds. Your array was defined with a size of 32 but you're trying to access 128 elements.

By the way the program is printing the values you assigned to the array for the first 32 elements. You only entered one number into the array (1) because you only asked for one number from the user, the rest of the 32 values are zero only because your array is a horrible global variable. And since you never actually store anything into ret, it is also zero because it is also a horrible global variable. You really should be creating those variables local to some function and learn to properly pass the variables to and from your functions as required.


Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7140
  • View blog
  • Posts: 24,250
  • Joined: 05-May 12

Re: Simple function, weird output

Posted 29 September 2019 - 06:23 AM

sizeof() operator returns the number of bytes, not the number of elements of the array. There are usually 4 bytes to an integer if you are using a modern compiler.
So what you are doing there is accessing an array out of bounds. Accessing an array out of bounds invokes undefined behavior. Welcome to your first experience of undefined behavior.
Was This Post Helpful? 1
  • +
  • -

#4 ghostname0x00   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-September 19

Re: Simple function, weird output

Posted 29 September 2019 - 06:25 AM

Okay, now it finally makes sense. Thank you and take care!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1