Home > Back-end >  signal: segmentation fault (core dumped) error
signal: segmentation fault (core dumped) error

Time:04-01

Wrote a program that multiply two arrays like this:

uv = u1v1 u2v2 u3v3 ... un*vn

after getting from the user both of the arrays, i get a "signal: segmentation fault (core dumped)" error.

here's the code:

#include <stdio.h>


int scalar_product(int vectorU[], int vectorV[], int vectorLength) {
  int i, sum = 0;
  for (i = 0; i < vectorLength; i  )
    sum  = (vectorU[i] * vectorV[i]);
  return sum;
}

void userInterface() {
  int vectorLength = 0, i;
  printf("Please enter the length of the vectors: ");
  scanf("%d", &vectorLength);
  int vectorU[vectorLength], vectorV[vectorLength];
  printf("\nVector U:");
  for (i = 0; i < vectorLength; i  ) {
    printf("\n%d) ", (i   1));
    scanf("%d", &vectorU[i]);
  }
  printf("\nVector V:");
  for (i = 0; i < vectorLength; i  ) {
    printf("\n%d) ", (i   1));
    scanf("%d", &vectorV[i]);
  }
  printf(scalar_product(vectorU, vectorV, vectorLength));
}

main(void) {
  userInterface();
}

CodePudding user response:

Here:

  printf(scalar_product(vectorU, vectorV, vectorLength));

... you have failed to specify a format for printf. As a result, it attempts to interpret the result of scalar_product() as a pointer to the format string. Undefined behavior results.

If your compiler is not emitting a warning about that then you should either learn how to turn up the warning level so that it does, or else get a better compiler. If your compiler is emitting a warning about it, then take this as a lesson that it is not safe to ignore compiler warnings.

Probably you wanted somethign more like this:

  printf("%d\n", scalar_product(vectorU, vectorV, vectorLength));

As a minor additional issue, you have forgotten the return type for main(). Your compiler is probably treating it as returning int, which turns out to be the right thing to do, but that doesn't make your code correct. You want:

int main(void) {
    // ...

With those two changes, your program compiles for me without any diagnostics, and runs without error, producing the result I expect.

At least, for small vector lengths. If you try really large vectors then you might exceed the available space for allocating your vectors on the stack.

CodePudding user response:

This call of printf is incorrect

printf(scalar_product(vectorU, vectorV, vectorLength));

You need to write at least

printf( "%d\n", scalar_product(vectorU, vectorV, vectorLength));

Also it would be better to declare and define the function like

long long int scalar_product( const int vectorU[], const int vectorV[], int vectorLength) {
  long long int sum = 0;
  for ( int i = 0; i < vectorLength; i  )
    sum  = ( long long int )vectorU[i] * vectorV[i];
  return sum;
}

To output the result you need to use the format string "%lld\n"..

The type long long int is used to avoid an overflow.

Another way is to declare the function return type as double.

Also you forgot to specify the return type int of the function main.

  • Related