1

I've been pulling my hair out over a block of code that I can't figure out how to fix, or even what's causing it to blow up in my face. I've been searching this site for people with similar problems, but I can't find one that's exactly like mine, and... well I'm just completely stumped.

int main() { 
    numOfTerms = 0;

    /*Getting length of sequence from user.*/

    printf("How many terms are in the sequence? ");

    scanf("%i", &numOfTerms);

    int terms[numOfTerms-1];

    /*Filling array with terms of the sequence, dictated by user.*/

    int integerChoice = 0;


    for (int i = 1; i < numOfTerms; ++i) {
        printf("Enter an integer: ");
        scanf("%d", &integerChoice);
        terms[i-1] = integerChoice;
    }
...

I don't have any trouble with the compilation, no warnings, no worries. But when I'm testing the program to make sure it works, I always reach the "Enter an integer: " prompt, and give it an integer only to be slapped in the face with "Segmentation fault: 11."

If it isn't already obvious, I'm... pretty new at this. I know (or at least I think know) what a segfault is. It's when your program swells up in terms of the amount of memory it uses to the point where it has to crash... right?

So why is that happening here? I already get past the point of allocating an array... which is where I'd assume the mistake is if a segmentation fault is this "ballooning" kind of problem. But no, it's happening at the lines that I suspect the least:

scanf("%d", &integerChoice);
terms[i-1] = integerChoice;

... Can anyone explain what's going on, and how I can fix it?

  • 2
    You haven't posted your actual code since the very first line won't compile. Where is the type of `numOfTerms`? – ooga May 07 '14 at 02:24
  • 3
    There's also an off-by-one error: if numOfTerms is 1, the program will allocate and ask for zero terms. (That's not _the_ problem, but it's _a_ problem.) – Adam Liss May 07 '14 at 02:26
  • 1
    I can't help but wonder if you're a little confused about how to deal with 0-based versus 1-based array indexing. So many "n-1"s... – Jim Lewis May 07 '14 at 02:28
  • 1
    @AdamLiss something similar happens if a negative number is entered, so perhaps it is an "off-by-up-to-INT_MAX" problem :) – M.M May 07 '14 at 02:29
  • @user3610400 make sure `numOfTerms` is `int` (otherwise your scanf specifier is a mismatch), and to help with your debugging, try outputting it with `printf` before allocating the array (again make sure to use the right specifier for printf). – M.M May 07 '14 at 02:33
  • also, compile with `-std=c99` , the GNU behaviour for variable-length arrays is different to Standard C's – M.M May 07 '14 at 02:35

1 Answers1

1

Here is a version that works:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numOfTerms = 0;

    /*Getting length of sequence from user.*/

    printf("How many terms are in the sequence? ");

    scanf("%i", &numOfTerms);

    int *terms = malloc(numOfTerms * sizeof(int)); // OR   int terms[numofTerms];

    /*Filling array with terms of the sequence, dictated by user.*/

    int integerChoice = 0;

    for (int i = 1; i <= numOfTerms; ++i) {
        printf("Enter an integer: ");
        scanf("%d", &integerChoice);
        terms[i-1] = integerChoice;
    }
    return 0;
}

Note:

  • The missing 'int' declaration of numOfTerms
  • The corrected dynamically sized memory allocation for the array of terms
  • The for loop stopped early (you need to use <= size if using a one-relative index)
Mike
  • 3,722
  • 1
  • 28
  • 41
  • BTW, I used a malloc as the solution to the second problem to give you a solution that works on all C compilers. On gcc if you use -std=c99 then you could solve the second problem by replacing `int *terms = malloc(numOfTerms * sizeof(int));` with `int terms[numOfTerms]` rather than `int terms[numOfTerms-1]` shown in the original question code. – Mike May 07 '14 at 13:29