0

I have an array with values initialized to an integer value. When I try to change the values and print to file, the code compiles but returns a "segmentation fault" error upon execution. Any thoughts would be much appreciated!

int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
  ...//code setting int "p" to some number between -100 and 100
  if (p < 25 || p > -25)
  {
  int temp = p + 25;
  int currentVal = theArray[temp];
  theArray[temp] = currentVal + 1;
  }
}

When I take out the step changing the "currentVal" there is no segmentation fault. Thanks in advance!

7
  • Your code now sets all values of the array to 1? Commented Feb 10, 2017 at 5:09
  • the way it is written here, yes. the actual version is actually different in several ways. i just was trying to isolate the problem based on what i've already tested out and "eliminated". would it be helpful if i changed it to make it more similar to the original? Commented Feb 10, 2017 at 5:14
  • 2
    Check that just the code you show in your question actually exhibits the problem you describe... what you have showing right now should have no problems, so something's likely missing or different that's causing your issue. Commented Feb 10, 2017 at 5:14
  • It'll be helpful cuz you don't know whats causing the error. Maybe the error is somewhere else Commented Feb 10, 2017 at 5:16
  • 1
    For a valid array index, p + 25 must be in the range 0..49, so p needs to be at least -25, and at most 24. So try, if (p < 25 && p >= -25) Commented Feb 10, 2017 at 5:47

5 Answers 5

3

Your condition is wrong

if (p < 25 || p > -25)

if p is 1000, it will enter this if, as well as when p is -1000. You need the AND logical operator

if (p < 25 && p > -25)

Also, since your valid indices range from 0 to 49 inclusive, I think one of the operators must include equality, namely:

if (p < 25 && p >= -25)
Sign up to request clarification or add additional context in comments.

Comments

0

You've never declared the size of the array, so it's an array of length 0 (no ints have been allocated). Therefore, then you try to initialize a part of the array, you get a segmentation fault due to accessing memory in which you are not allowed to.

To fix this, give the array a compile-time constant array size:

int theArray[50];
...//code setting the array values to zero
int i;
for(i = 0; i < 50; i++)
{
  int currentVal = theArray[i]; // make sure you've initialized the array before doing this
  theArray[i] = currentVal + 1;
}

3 Comments

Hi! I actually did in the original code, I just tried to isolate the question here and in my rush forgot to add the size. I will update it here though! Thanks!
@smac89 i do not think i have edited an answer, just my original question by adding the [50]. i was planning on editting the question again to make it more accurate to the problem which i hope would be helpful for myself and anyone else who ends up on this thread for this reason. if this is not proper etiquette please let me know. (i also thought i had posted that last part just a moment ago but now i cant find it... im a little new here) update: it refreshed and was actually there - deleted it to remove redundancy! wow, i am bad at this!
@sparkyrules77, oops that was my mistake. I apologize
0

While giving the input you must have entered integers more than the size of your array theArray[] i.e, 50.That is why your compiler is showing you segmentation fault.

1 Comment

i thought this too! i thought the if statement would be sufficient to make sure that it did not look at array values below 0 or above 50 though... Also, when i just reset the value to zero it doesnt complain
0

I am almost sure it be if(p<25 && p>-25)... You probably confused the logical operators.

Comments

0

Isn't this is what you are trying to do ?

int i;
for(i = 0; i < 50; i++) {
    theArray[i] += 1;
}

Does this not work for you ?

EDIT

if (p < 25 || p > -25) will evaluate to true for values such as -50, -26, etc because of short circuit evaluation of ||, meaning p < 25 will be evaluated first and if found true the other condition will not be evaluated.

For numbers like 50, the second condition will evaluate to true and temp will again go out of bounds for theArray

In either case, you will see Segmentation fault

if (p < 25 && p > -25) should help you.

4 Comments

sadly no, i am going to update the code that i shared to be more accurate to the bigger picture and the actual function.
It's not just because of short-circuit evaluation -- any number will make at least one of the conditions true, so the OR of them will be true. That'd be the case even without short-circuit evaluation.
thank you! that was one problem, and i had also been trying to access a value later on that did not exist! :) debugging complete!
@sparkyrules77, you could have used a debugger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.