user12812187
user12812187

Reputation: 45

Why am I getting a segmention fault?

For my class, we are using two compilers, xcode and codeboard, where codeboard is the more rigorous of the two and has a list of preset asserts to test your code. In my code for function a, I have nested loops, where I want i to always be one ahead of k. It works fine in xcode, but gives me a segmentation fault on codeboard.

for (int k = 0; k <= n-2; k++) {
    for (int i = k+1; i <= n-1; i++) {
        if (array[k] == array[i])

What's strange is if I use k++ or ++k instead of k+1, it won't result in a segmentation fault, but my code won't pass all the asserts. I thought they were supposed to be the same thing?

What's even more strange is if I use k+1 in function b, it doesn't result in an error in codeboard.

for (int k = 0; k <= n-1; k++) {
    for (int i = k+1; i <= n-1; i++) {
        if (array[k] > array[i])

Basically, what is happening here?

Edit: So, what I'm getting from everyone is that when I use i = k+1 in the inner loop, it increases the k value in the outer loop. Like, if k=1, i=2, then when it goes back to the outer loop, k=3. If that's the case, how would I get i from staying ahead of k?

Edit: Please ignore the k++ and k+1 part of my question, that was not really my main issue, just a side thing that kind of confused me, but I get the difference now. I'd delete that part from my post, but I'm pretty sure that's not allowed. My main question is how to keep my i value 1 greater than my k value.

In the following code, the goal is if any of the array values are the same, it should cout true at some point. In regards to reproducible results:

string a[3] = {"aaa", "bbbb", "cccc"};
int n = 3;
for (int k = 0; k <= n-2; k++) {
    int b = 0;
    for (int i = k+1; i <= n-1; i++) {
        if (a[k] == a[i])
            b++;
    }
    if (b != 0) {
        cout << "true" << endl;
        break;
    }
    else
        cout << "false" << endl;
}

Upvotes: 0

Views: 282

Answers (3)

k++ and k+1 are not the same.

If you use k++ instead of k+1 inside the loop in the code you have shown, you are actually performing two increments on the variable k, which is usually not what you want in the control variable of a loop.

  • K+1 does not alter the value of k.
  • k++ increments k after using its value.
  • ++k increments k before using its value.

So basically what is happening is that every time you start the inner loop, k gets incremented. But in the last round of the external loop, when k is equal to n-1, you perform another increment, which sets k to n, and this is most certainly not what you wanted.

Upvotes: 0

Frederik Juul
Frederik Juul

Reputation: 301

k++ and ++k both increase k by one, changing the value of k.

k+1 does not.

Your segmentation fault comes from the fact that you are increasing k every time your initial for-loop is evaluated but also every time your second for loop is initialized. This means that you're reading further than your array at the end of your loop.

Upvotes: 0

Rene
Rene

Reputation: 2474

No, they're not the same:

k + 1
Returns the value of k plus one, but does not change the value of k itself.

++k
Adds 1 to k and returns the new value (pre-increment).

k++
Also adds 1 to k but returns the previous value of k (post-increment).

Upvotes: 3

Related Questions