Weird segmentation fault

Pages: 12
so im making an array of size K and then a for loop will put numbers in the array in descending order but im getting a segmentation fault in my for loop.
1
2
for(int i=K;i>=0;i--)
           c[K-i]=i%100;


anyone have a clue as to why?
May I see the declaration of c?

Edit: Actually, I think I might need to see the whole code. The value of K is apparently not within the bounds of your array.
Last edited on
You're making that a lot harder than it needs to be, though I'm guessing you have your reasons. :|
for(int i=K;i>=0;i--)

When i = 0, what is K - i going to be equal to? Remember, array indexes go only up to size - 1.

-Albatross
Last edited on
ahhhh ok yea thats it thank you haha i knew it was something obvious
change your condition from a >= sign to a > sign
well here is what my main is atm and its still faulting in the same place, but would i want to keep the >= so it puts a number in the sot zero of the array?
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
43
44
45
46
47
48
49
50
51
52
53
int main()
{
       
       const int K = 100000; // Number of items to be sorted.
       unsigned seed = time(0);

       // Seed the random number generator.
       srand(seed);
           
       double t1, t2,elapsed_time;
	int a[K];
	int a2[K];
	
	int b[K];
	int b2[K];
	
	int c[K];
	int c2[K];
	
	for(int i=0;i<K;i++)
           a[i]=rand()%100;
    
    for(int i=0;i<K;i++)
            b[i]=i%100;
            
    for(int i=K-1;i>0;i--)
            c[(K-1)-i]=i%100;
         
    t1=clock();
	mergeSort(a, a2, K);
    t2=clock();
    elapsed_time=t2-t1;
    cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
	print(a,K);
	
	
	
	t1=clock();
	mergeSort(b, b2, K);
    t2=clock();
    elapsed_time=t2-t1;
    cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
	print(b,K);
    
    t1=clock();
	mergeSort(c, c2, K);
    t2=clock();
    elapsed_time=t2-t1;
    cout<<"The elapsed time was "<<elapsed_time/CLOCKS_PER_SEC<<" seconds"<<endl;
	print(c,K);
    system("PAUSE");
	return 0;
}//end main 

Last edited on
I tried your code and I'm having memory issues. Try using shorts instead of ints for your arrays.

1
2
    for(int i=K-1;i>0;i--)
            c[(K-1)-i]=i%100;

Also, here you won't access the last element of your array, despite what ascii said I believe you want i >= 0, or c[K-i] as Albatross said below.
Last edited on
Aw, why'd you have to change the K to (K-1) in that loop? Now you're missing the element at the 99999th position. :(

I'm still not sure why you had to make the loop the way you did.

That aside, I can't put my cursor finger on any obvious faults in your code.

-Albatross
Albatross how would you code it? what i did seemed to be the most trivial way to me to do it.
If I wanted a loop that iterated over an array from 0 to size - 1, I might do what you did in the loops for a and b.

If I wanted a loop that iterated over an array from size - 1 to 0 (is this what you wanted?), I might consider writing something like this:
1
2
for(int i = K - 1; i >= 0; --i)
    c[i] = (K-1-i)%100;


I see why you had the loop you had.

-Albatross

EDIT: Ack, missed a question mark.
Last edited on
i tried changing it to
1
2
3
4
5
6
int tmp=K;       
    for(int i=K;i>=0;i--)
    {
            c[i]=tmp;
            tmp--;
    }

but i still get a seg fault
Did you try what I said? ^see above^

Edit: If that doesn't work you could even try using char.
Last edited on
so after further investigation the segmentation fault ends when i remove the declaring of int c[];and c2[];
yea i havnt im trying to see if i can fix it with out doing that because thats alot of code i have to change to make them shorts
No, it's not a lot of code to change, you only have 6 arrays ;). You could use Find and Replace if you wanted.
i have to change all of my functions as well to work with shorts instead of just ints
ok so the problem was it doesnt like me making 6 arrays of size 100000
Find&Replace is great for this sort of thing. I really think this will resolve your problem.

And yes, that is the problem, since shorts use less memory, it will probably work. At least it worked when I tried it.
Last edited on
cant a short int only go up to the number like 40,000 or something like that though? so if thats the case then i cant use 100000 anyway
a[i]=rand()%100;
You're values only go from 0-99.
const int K = 100000;
This will still be an int, and can hold the value of 100000.

So shorts will still work, with less memory, and therefore little chance of a seg-fault unless your computer is a dinosaur.

@Anybody: Can you please back me up on this? The OP doesn't seem to believe me :-(.

Again:
I wrote:
it worked when I tried it.

Of course, I could only test your code up to line 29, but guess what. I didn't get a seg-fault!
Last edited on
Pages: 12