# Simple function, weird output

Page 1 of 1

## 3 Replies - 209 Views - Last Post: 29 September 2019 - 06:25 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417432&amp;s=c5eaecc1eef5af209c6d51fd286616dc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ghostname0x00

Reputation: 0
• 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

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

Reputation: 5771
• Posts: 17,671
• 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

### #3 Skydiver

• Code herder

Reputation: 7139
• Posts: 24,245
• 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.

### #4 ghostname0x00

Reputation: 0
• 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!