Variable Declarations and Array Boundaries

Nov 28, 2018 at 5:39pm
In the lines under the comment labeled Section 1, what is incorrect about these variable declarations?
Rewrite the lines so that they are valid C++ statements that will compile. Use your best judgment on the values to use in place of the incorrect ones.
Under Section 2, is there a problem with these lines of code (check your array boundaries)? If so, how do you fix them?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int size;

	//Section 1 - Array Definitions
	int readings[-1];
	float measurements[4.5];
	string names[size];

	//Section 2
	const int SIZE = 100;
	int numbers[SIZE];
	for (int i = 1; i <= SIZE; i++)
		numbers[i] = i;

system("pause");
return 0;

}


I understand that the declarations cannot be less than zero or a decimal, but how do I change this to get it to compile?
Nov 28, 2018 at 5:54pm
How can you possibly have an array of size -1?

How can you possibly have an array with 4 and a half elements in?

What size will your names array be?

how do I change this to get it to compile?

Give those arrays sensible sizes.
Last edited on Nov 28, 2018 at 5:54pm
Nov 28, 2018 at 6:07pm
Yes, I understand everything you have said and detailed that in my initial post.

After changing everything, it still does not compile correctly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using namespace std;

int main()
{
	int size;

	//Section 1 - Array Definitions
	int readings[6];
	float measurements[4];
	string names[7];

	//Section 2
	const int SIZE = 100;
	int numbers[SIZE];
	for (int i = 0; i <= SIZE; i++)
		numbers[i] = i;

system("pause");
return 0;

}
Nov 28, 2018 at 6:10pm
it still does not compile correctly

If it doesn't compile, your compiler will be giving you useful messages indicating what's wrong. Why would you decide not to pass those on to us, to help us find your problem?

I can't see any compilation problems, but I can see a runtime issue - your loop will attempt to write data to memory past the end of your numbers array. Can you see why?
Last edited on Nov 28, 2018 at 6:19pm
Nov 28, 2018 at 6:15pm
Because I am totally new to this. I apologize for my lack of knowledge. It now compiles, but displays no information.
Nov 28, 2018 at 6:17pm
If you've changed the code from the last time you posted it, can you show us the latest version?

Nov 28, 2018 at 6:18pm
The last code I posted is up to date.
Nov 28, 2018 at 6:20pm
OK. So, as I said previously:

MikeyBoy wrote:
your loop will attempt to write data to memory past the end of your numbers array. Can you see why?

Nov 28, 2018 at 6:24pm
No, I cannot see why. Like I said, I am very new to all of this. It's still very foreign to me.
Last edited on Nov 28, 2018 at 6:25pm
Nov 28, 2018 at 6:30pm
In C and C++, array indices start at 0. So, the first element of your numbers array is numbers[0], and the last is numbers[99].

In your loop, the value of i during the last iteration will be 100. So, in that final iteration, you'll be trying to write to numbers[100] - which doesn't exist. It is writing memory past the end of the array, which causes what we call "undefined behavour" - i.e. something that you can't predict, and verry possibly a crash.
Last edited on Nov 28, 2018 at 6:30pm
Nov 28, 2018 at 6:50pm
Thank you so much for the explanation. That makes total sense!

The program is not displaying info, and I am getting an error that says "Stack around the variable 'numbers' was corrupted
Last edited on Nov 28, 2018 at 6:51pm
Nov 28, 2018 at 6:51pm
Yes, that would fit perfectly with the problem I described. Specifically, the memory just after the end of numbers is getting corrupted.
Last edited on Nov 28, 2018 at 6:51pm
Nov 28, 2018 at 6:56pm
Great. I have changed the 100 to a 99, but I'm still getting no info to display.

I thank you for your time.
Nov 28, 2018 at 6:59pm
What info would you expect to be displayed? You haven't written anything in your code that would display anything.

Take a look at http://www.cplusplus.com/doc/tutorial/basic_io/ if you want an introduction to how to make your program output things.
Topic archived. No new replies allowed.