Variable Declarations and Array Boundaries

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?
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
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;

}
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
Because I am totally new to this. I apologize for my lack of knowledge. It now compiles, but displays no information.
If you've changed the code from the last time you posted it, can you show us the latest version?

The last code I posted is up to date.
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?

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
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
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
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
Great. I have changed the 100 to a 99, but I'm still getting no info to display.

I thank you for your time.
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.