Program keeps crashing

Hello everyone!
I'm having a bit of a problem with one of the classes I'm making.
Here is my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sequence::insert(const value_type& entry){
	if(size() < CAPACITY){
		if(cPos == 30 || used == 0){
				a[used] = entry;						
				cPos = used;
				used++;
		}
		else{
	                for(int i = used; i >= cPos; i--) {a[i+1] = a[i];}	//Shift everything once
			a[cPos] = entry;
			used++;
		}
	}
}


This function crashes the program whenever cPos(current item position) = 0. I cant figure out why either. i checked to see if it was accessing memory outside of the array but its not. what the for loop is suppose to do is shift everything until a[cPos -1] to the right. for example:
a[CAPACITY] = [20, 30, 40, 0, 0, 0]
When cPos = 2 and after the for loop the array should look like this after inserting 50
a[CAPACITY] = [20, 30, 50, 40]

But when cPos is 0 the program crashes. Would you guys happen to know what i did wrong?

Thank you!
In else part your doing used++; and again in for loop it will initialized int i = used; it seems like program will never end and will crash...
please use, that may be useful to you.
static int i = used;
For which values of used does it crash?
What does the size() function return? Does it just return used?
I would see a crash when used == CAPACITY - 1 then in the first iteration of the for loop you would write into a[CAPACITY] which is not correct. If it is the case, you need to rewrite the for loop as: for(int i = used - 1; i >= cPos; i--).
If it is not - maybe there's a problem with class member initialization, but you'd have to post the rest of your code for us to see where is the problem.

Also this kind of problems should be easily fixed using a debugger and step-by-step execution. Did you try that?
@HiteshVaghani1: used++ is not a part of the loop. And even if it were, the initialization is only done one for a for loop.
This code snippet does not crash just because cPos is zero, that's not the reason.

I wonder what is the meaning of the two variables cPos and used? Because you're using both as indices to access data in your array a, which i find quite confusing. Did you intend that?

Also consider the situation if used is equal to the maximum capacity and cPos is 30. In this case the if statement evaluates to true and it will try to access a[used], although used is out of range (which is 0...used-1).

Btw. i don't see how making i static will solve the problem.
Sorry for being a bit confusing.

@KRAkatau:
size() returns the size of the array 'a' which is also the value of usedalso used would never be greater than or equal to because of this
if(size() < CAPACITY)

Ill try out the debugger and see if i can spot the problem.

@AleaIactaEst:
The variable cPos is the position of the current value and used is the amount of numbers that is in the array.

Thanks for you help guys if i cant spot the problem with the debugger ill add more code for you to see.
I tried to debug and i noticed that after the for loop the array start get filled with a bunch of random numbers... but i still cant figure out why.

ill post up the header file and the constructor function.

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class sequence
    {
    public:
        // TYPEDEFS and MEMBER CONSTANTS
        typedef double value_type;
        typedef size_t size_type; //typedef std::size_t size_type;
        static const size_type CAPACITY = 30; // enum {CAPACITY = 30}; //
        // CONSTRUCTOR
        sequence( );
        // MODIFICATION MEMBER FUNCTIONS
        void start( );
        void advance( );
        void insert(const value_type& entry);
        void attach(const value_type& entry);
        void remove_current( );
        // CONSTANT MEMBER FUNCTIONS
        size_type size( ) const;
        bool is_item( ) const;
        value_type current( ) const;
    private:
		size_type used;
		size_type cPos; 
		value_type a[CAPACITY];
    };


Constructor:
1
2
3
4
5
6
sequence::sequence(){
			used = 0;
			cPos = 30;	//30 as no current item
			//zero out the array
			for(int i = 0; i < CAPACITY; i++){a[i] = 0.0;}	
		}


If you guys need any more information feel free to ask.
Thank You!
First a design issue: In my opinion it would be more elegant to replace the the typedefs and CAPACITY with template parameters.

Also, if used is equal to CAPACITY-1, you'll try to access a[used+1] = a[CAPACITY] in the for loop, this might be the reason why your program crashes. Other than that i cant find anything wrong in the code.

If this doesn't help i suggest to post the portion of the code where you create an instance of the sequence class and call the methods, the error might be hidden somewhere there.
I got it to work. I changed the for loop to this

1
2
3
for(size_type i = used; i > cPos; i--) {
	a[i] = a[i-1];
}		


Thanks everyone for your help!
Topic archived. No new replies allowed.