Dynamic array (with negative indeces)

OK, everything looks alright to me, but I'm pretty green when it comes to this. It is compiling and running fine, but it isn't outputting what I am expecting. Maybe I'm missing something? Thanks ahead of time for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>

using namespace std;

int main()
{
	int arraySize;
	int negValue;
	int counter;
	int counter2 = 0;	
	
	cout << "Please input the size of the array you want: ";
	cin >> arraySize;
	int negIndexArray[arraySize];
	
	cout << "Please input how far you want to go into negative in indeces: ";
	cin >> negValue;
	
	int* point = negIndexArray+negValue;	//index into the array
	
	for (counter = negValue; counter<arraySize && counter>negValue; counter++)
		point[counter] = counter2;
		counter2++;

	cout << point[negValue];
	cout << point[arraySize];
	
	return 0;
}
Line 14 is illegal and should not compile.

Also, negative indicies are the memory before the array, which is undefined behavior to access. Most likely you are overwriting other variables.
Well it compiles and runs fine...I know negative indices are memory before the array. I know it can be done though, it's an assignment in my book. Maybe I'm just going about it the wrong way?
cphipps wrote:
Well it compiles and runs fine...
That's no indication of valid code. There are plenty of invalid programs that "compile and run fine" on some compilers.

Could you quote the exercise specifically? I have a feeling you're misunderstanding it (or maybe it is trying to show you what memory corruption can cause).
I know this also, I'm green, but it isn't my first rodeo. I was saying that because you said it shouldn't compile.

Here is the assignment though:

Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0. Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type myArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appro-priate error message. Consider the following statements: myArray<int> list(5); //Line 1 myArray<int> myList(2, 13); //Line 2 myArray<int> yourList(-5, 9); //Line 3 The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], ..., list[4]; the statement in Line 2 declares myList to be an array of 11 com-ponents, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8]. Write a program to test the class myArray.
Ah, the assignment want you to make a class that allows for a different starting index than 0 and to disallow accessing out-of-bounds locations.
Yes...I guess I should've been more specific for the purpose of my program. I didn't quite fully understand it myself though. I have completely abandoned the above code though, and have started another way. I think I have it working fine, at the moment at least. Thanks for the assistance!
Edit:
False Alarm! I have figured it out! There were several errors just for the record. I just had to walk away for a bit.


OK, I abandoned my old code, and have started fresh.
Here is the error I'm getting:
11 4 G:\USB Files\USB Files\Spring 2014\C++ Homework\Session 10\myArrayHeader.h [Error] expected unqualified-id before '{' token

Anyone see what I'm missing?
Thanks for any help!

Code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdlib>
#include "myArrayHeader.h"

int main()
{

	intArray mainArray;
	int i=0;
	std::cout<<"Type the index of the array element you want to see or 1234 to stop the program: "<<std::endl;
	while (i!=1234)
	{
    	std::cin>>i;
    	std::cout << mainArray[i] << std::endl;
	}
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef myArrayHeader_H
#define myArrayHeader_H
#include <iostream>
#include <cstdlib>
//int listArray[10];
class intArray
{
	public:
		intArray();
		int listArray[10];
			{
				int *negList;
				negList = list;
				*(negList-5)  = 2;
				*(negList-4)  = 5;
				*(negList-3)  = 4;
				*(negList-2)  = 1;
				*(negList-1)  = 0;
				*(negList+0)  = 1;
				*(negList+1)  = 6;
				*(negList+2)  = 8;
				*(negList+3)  = 9;
				*(negList+4)  = 1;
			}

	int &operator[](int i);
};
#endif
/*
// Provide range checking for intArray.
int &intArray::operator[](int i)
{
if(i<0 || i> 9) {
cout << "Boundary Error\n";
system ("pause");
exit(1);
}
return list[i];
}*/



1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "myArrayHeader.h"
using namespace std;

int &intArray::operator[](int i)
	{
	if(i<0 || i> 9) {
	cout << "Error: Out of bounds!\n";
	exit(1);
	}
	return list[i];
}
Last edited on
WHat are lines 11 to 24 supposed to be for in your header? They're not attached to anything and they won't work anyway.
Topic archived. No new replies allowed.