Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 此代码中的分段错误 #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效排序(int a[],int n) { int i,j; 对于(i=0;i0) { h[i]=hit; 如果(h[i]_C_Segmentation Fault - Fatal编程技术网

C 此代码中的分段错误 #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效排序(int a[],int n) { int i,j; 对于(i=0;i0) { h[i]=hit; 如果(h[i]

C 此代码中的分段错误 #包括 #包括 #包括 #包括 #包括 #包括 #包括 无效排序(int a[],int n) { int i,j; 对于(i=0;i0) { h[i]=hit; 如果(h[i],c,segmentation-fault,C,Segmentation Fault,请求调试器或使用。 它会自动检测内存管理错误。它还会详细分析你的程序 使用Valgrind太简单了。请编辑您的问题。标记您正在使用的语言。添加问题。解释发生了什么和什么错误。解释在调试程序中逐行运行代码检查错误时发生了什么。您似乎没有任何数组访问权限。请尝试查看您的调用堆栈。是否有错误有什么不寻常的地方吗?我稍微调整了代码格式。我没有遇到非法内存访问错误,但我确实遇到了堆栈溢出,因为call进入无限递归。也许你应该共享触发崩溃的确切参数值。a[j]=a[j+1](a[j+1]=a[j]);:此处

请求调试器或使用。 它会自动检测内存管理错误。它还会详细分析你的程序


使用Valgrind太简单了。

请编辑您的问题。标记您正在使用的语言。添加问题。解释发生了什么和什么错误。解释在调试程序中逐行运行代码检查错误时发生了什么。您似乎没有任何数组访问权限。请尝试查看您的调用堆栈。是否有错误有什么不寻常的地方吗?我稍微调整了代码格式。我没有遇到非法内存访问错误,但我确实遇到了堆栈溢出,因为
call
进入无限递归。也许你应该共享触发崩溃的确切参数值。
a[j]=a[j+1](a[j+1]=a[j]);
:此处可能缺少一个序列点。
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void sort(int a[], int n)
{
  int i, j;
  for (i = 0; i < n - 1; i++)
    for (j = i; j < n - 1; j++)
      if (a[j] > a[j + 1])
        a[j] = a[j + 1] - (a[j + 1] = a[j]);
}

int call(int i, int hit, int t, int h[], int n, int count)
{
  static int max = 0;

  if (t <= 0)
  {
    if (count > max)
      max = count;
    return max;
  }

  if (h[i] > 0)
  {
    h[i] -= hit;
    if (h[i] <= 0)
      count++;
    max = call((i + 1) % n, hit, t - 1, h, n, count);//this is line where illegal access to memory is being made
    count--;
    h[i] += hit;
  }

  max = call((i + 1) % n, hit, t, h, n, count);
  return max;
}

int getMaxMonsters(int n, int hit, int t, int h[]) {
  sort(h, n);
  int m = call(0, hit, t, h, n, 0);
  return m;
}

int main() {
  int n;
  int hit;
  int t;
  scanf("%d %d %d", &n, &hit, &t);
  int h[100000];

  for (int h_i = 0; h_i < n; h_i++) {
    scanf("%d", &h[h_i]);
  }

  int result = getMaxMonsters(n, hit, t, h);
  printf("%d\n", result);
  return 0;
}