A Pointer Bug ?

when test7() is call in main, it sucessfully create the first object (IntArray f(1, 4);) BUT will only get to "currentArray = new int[(indexz - 1)];" in arrayInit(). when try IntArray g(5,8);. the error is "No source available for "__kernel_vsyscall() at 0x12d422". I've read some posts regarding null pointer in the forum, but I don't see it being null here. MUCH appreciated if someone could point me the right way.

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
40
41
42
 CONSTRUCTOR:
IntArray::IntArray(int a, int b) {
	arrayStart = a;
	arrayEnd = b - 1;

	if (a != 0) {
		arrayEnd = b;
		arrayStart = a;
		initArray(a,b);
	}
	else if(a == b) {
		arrayEnd = 0;
		arrayStart = 0;
		initArray(arrayEnd);
	}
	else {
		arrayStart = 0;
		arrayEnd = (b - a) - 1;
		initArray(arrayEnd);
	}
}

void IntArray::initArray(int a, int indexz) {
	currentArray = new int[(indexz - 1)];
	for (int x = a; a <= indexz; a++) {
		currentArray[a] = 0;
	}
}

int main() {
	test7();
}

void test7() {

    IntArray f(1, 4);
    for(int i = f.low(); i <= f.high(); i++)
        f[i] = i * 10;
    cout << f << endl;

    IntArray g(5, 8);
}
Last edited on
Does that compile?

The initArray function takes 2 arguments, you are only supplying 1 on lines 14 & 19.

Line 25 is OK, but IMO it would be better if you didn't use the <= operator, as this can often lead to going out of bounds of the array.

24
25
26
27
	currentArray = new int[(indexz)];
	for (int x = a; a < indexz; a++) {
		currentArray[a] = 0;
	}


Do you have to have an array, why not a vector?

What is it that you are trying to do? There might be an easier way .......
compiled, runs just fine until it gets to the second object ...got pass the constructor and die trying to create the array ! if it run fine with the first object, then why not the second ??? since both objects are in the same scope, something had to be left from the first object that's causing the problem right ? BUT I can't see it !

I am pretty sure there's an easier way... but I am very new to C++ and learning about array, pointer right now.
Last edited on
I'm not at all sure about this but you never initialize indexz, so it would seem to me that it is assigned a random int, therefore the loop ends before it finishes creating the object. Just a guess I'm not sure
Have a go with the debugger - what are the values of the variables when it crashes?

Actually lines 24 & 25 are a problem. line 24 creates an array with size index -1, while line 25 loops to index + 1, which becomes out of bounds on line 26.

When you create f(1,4) an array with 3 elements is created, then with the elements with subscripts 1, 2, 3, 4 you attempt to set to zero. But the legal subscripts are 0,1,2. So you are out of bounds by 2. I am having trouble seeing how this actually worked for you.

With the creation of g(5,8) an array with 7 elements is created - subscripts 0 to 6. You then try to set the values for elements 5 to 8.

The usual idiom for dealing with arrays looks like this :

1
2
3
4
5
6
7
const int SIZE = 5;

int MyArray[SIZE];

for (int a = 0; a < SIZE; a++) {
     MyArray[a] = 0;
}


Lines 14 & 19 are still a problem, unless you have another overloaded version of initArray that accepts 1 argument. Again I am having a hard time to see how this even compiled for you. What warnings have you got set in your compiler & what sort of compiler is it?

Btw, it would be better if you don't create 2 topics for the same questions. People will reply to both, it is annoying when one makes a long helpful reply, only to find the exact same things have been said in the other one.

Choose which topic you are going to continue with - presumably this one. Put a note directing everyone to this topic, in the other one.

HTH
Last edited on
Thanks HTH...this helped greatly.
when you say f(1,4), are you trying to put 1,2,3,4 into the array? g(5,8), 5,6,7,8 into the array?
@nmn

HTH means "Hope This Helps", not my initials 8+D (Big smiley face with glasses). There are others like IMO - "In My Opinion", and AFAIK - "As Far As I Know"

I am still interested to hear which compiler you are using, and which Warning Levels are set.

@jrfrago

That was what I was explaining.
If that is so, with (a) being the first and (b) being the last element of the array...allocation should be from 0 to (b-a) with the assumption that (b >=a) and would have (b-a+1) elements.
Last edited on
Hi jrfrago,
f(1,4) would be to have an array of 4 elements with the first indice being 1.
Here is my question for you.
lets say:
f(4,8) results in A[5]; //A[0],A[1],A[2],A[3],A[4] Right ?

then I do this:
a=4, b=8
if I loop through this range (4,8)...
for(int x = a; x<b; x++){
A[x] = 0; // should it be out of bound when x = 5 ?
// or the compiler counts the elements and not the index number ?

}


HTH,
I am running eclipse indigo on ubuntu 12.04.
G++ compiler didn't give any warning, but crash when run. I wonder if this article is valid here. http://everything2.com/title/negative+array+indexing
Last edited on
nmn wrote:
HTH,
I am running eclipse indigo on ubuntu 12.04.
G++ compiler didn't give any warning, but crash when run.


TheIdeasMan wrote:
HTH means "Hope This Helps", not my initials 8+D (Big smiley face with glasses). There are others like IMO - "In My Opinion", and AFAIK - "As Far As I Know"


If you would like to direct your comments to someone then use @ and their user tag as in @TheIdeasMan

Now for your compiler (you didn't say which version of gcc 4.8.1 is the latest):

Set The Warnings to a high level, I routinely use at least these flags :

g++ -std=gnu++11 -Wall -Wextra -pedantic

The gnu++ enables the gnu c++ extensions, can also use -std=c++11.
-Wall turns on a lot of warnings but not all of them cryptically.
-Wextra turns on more warnings not enabled by -Wall.
-pedantic turns on even more warnings.

You can also use -Werror which upgrades warnings to errors, and -pedantic-errors, which does the same for pedantic warnings.

It's all in the documentation - in the shell type man gcc there are a zillion options but it's worth reading what they mean.

I use KDevelop or QtCreator as my IDE, depending on what I am doing. KDevelop is a mature application which a lot of good features IMO. I use QtCreator if I want to do a GUI app, and this has a fabulous amount of features & technologies it can use, and can cross compile for different OS's.

nmn wrote:
then I do this:
a=4, b=8
if I loop through this range (4,8)...
1
2
for(int x = a; x<b; x++){
A[x] = 0; // should it be out of bound when x = 5 ? 


Yes, that was what I was saying, and is the reason for your problem, because valid subscripts are 0,1,2,3,4 when you declare A[5].

You could also investigate using a debugger - this is by far the easiest way to diagnose runtime errors. You can step through the code 1 line at a time, keep an eye on the value of the variables & deduce where it all goes wrong.

Your IDE should hopefully have a GUI interface to the debugger.

Hope all goes well.
@nmn -- for any given (a) and (b), your loop to get the range (a) to (b) into the array should like:

for (x=0; x<=(b-a); x++) {
array[x]=a+x; }

//from (a) to (b) there are b-a+1 elements. Thus, the array referencing is from 0 to (b-a).

again, with the assumption that b>=a.
----------
with what you were trying to do:

then I do this:
a=4, b=8
if I loop through this range (4,8)...
for(int x = a; x<b; x++){
A[x] = 0; // should it be out of bound when x = 5 ?
// or the compiler counts the elements and not the index number ?

a=4, b=8 would have 5 (b-a+1) elements which are 4, 5, 6, 7, 8
the first index being 0 and the last index 4 (b-a) being 0, 1, 2, 3,4

your iterator x starts at a= 4, thus A[4] --the last array index. Then the iterator goes to 5 and that is out of index.

Maybe you are confused with the array element referencing. Inside the bracket is the index, not the value.

If you declare an array A[5], that would have 5 elements with indices 0 to 4. After the declaration however, when you write A[3] it doesn't mean that it would have 3 elements but it references to the element with index 3 which is the 4th.

Last edited on
Topic archived. No new replies allowed.