信号:分割故障(核心倾倒)错误

发布于 2025-01-18 13:03:45 字数 954 浏览 5 评论 0原文

编写了一个将两个数组相乘的程序,如下所示:

uv = u1v1 + u2v2 + u3v3 + ... un*vn

从用户处获取两个数组时,我收到“信号:分段错误(核心转储)”错误。

这是代码:

#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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不气馁 2025-01-25 13:03:45

printf 的这种调用是不正确的

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

您至少需要编写

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

此外,最好声明并定义函数,如

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;
}

要输出结果,您需要使用格式字符串“%lld \ n”..

long long int 类型用于避免溢出。

另一种方法是将函数返回类型声明为 double。

您还忘记指定函数 main 的返回类型 int

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.

牵强ㄟ 2025-01-25 13:03:45

这里:

 printf(scalar_product(向量U,向量V,向量长度));

...您未能指定 printf 的格式。因此,它尝试将 scalar_product() 的结果解释为指向格式字符串的指针。未定义的行为结果。

如果您的编译器没有发出有关此问题的警告,那么您应该学习如何调高警告级别以使其发出警告,或者获得更好的编译器。如果您的编译器正在发出有关它的警告,那么请将此作为一个教训,忽略编译器警告是不安全的。

也许您想要类似这样的东西:

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

作为一个小附加问题,您忘记了 main() 的返回类型。您的编译器可能将其视为返回 int,事实证明这是正确的做法,但这并不意味着您的代码正确。您想要:

int main(void) {
    // ...

通过这两项更改,您的程序可以在没有任何诊断的情况下为我编译,并且运行时没有错误,产生我期望的结果。

至少对于小向量长度来说是这样。如果您尝试非常大的向量,那么您可能会超出堆栈上分配向量的可用空间。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文