Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.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/8/file/3.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 程序没有错误,但仍然崩溃_C - Fatal编程技术网

C 程序没有错误,但仍然崩溃

C 程序没有错误,但仍然崩溃,c,C,这段代码运行良好,也会生成所需的输出,但最终会弹出此窗口 可以看出,未提及任何错误或警告。我想纠正这一点。 它肯定不会卡在循环中,因为它正确地返回了所需的值。这个问题背后的原因可能是什么?请帮忙 代码如下: #include <assert.h> #include <limits.h> #include <math.h> #include <stdbool.h> #include <stddef.h> #include <std

这段代码运行良好,也会生成所需的输出,但最终会弹出此窗口

可以看出,未提及任何错误或警告。我想纠正这一点。 它肯定不会卡在循环中,因为它正确地返回了所需的值。这个问题背后的原因可能是什么?请帮忙

代码如下:

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define size 10000

int max(int a, int b)
{
    if(a>b)
        return a;
    return b;
}

int find_subseq(char *a, char *b)
{
int i, j, la = strlen(a), lb = strlen(b);
int *arr = (int *)malloc(((la+1)*(lb+1)*sizeof(char)));

for(i = 0; i < la; i+=(lb+1))
{
    *(arr+i) = 0;
}

for(j = 0; j < lb+1; j++)
{
    *(arr+j) = 0;
}

for(i = 1; i <= la; i++)
{
    for(j = 1; j <= lb; j++)
    {
        if(*(a+i) == *(b+j))
            *(arr+i*lb+j) = *(arr+i*(lb-1)+(j-1)) + 1;
        else
            *(arr+i*lb+j) = max(*(arr+i*(lb-1)+(j)),*(arr+i*(lb)+(j-1)));
    }
}
return *(arr+la*lb+lb);
}

int main()
{
    char* a = (char *)malloc(size*sizeof(char));
    scanf("%[^\n]%*c",a);

    char* b = (char *)malloc(size*sizeof(char));
    scanf("%[^\n]%*c",b);

    printf("%d",strlen(a) + strlen(b) - (2 * find_subseq(a,b)));

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义大小10000
最大整数(整数a,整数b)
{
如果(a>b)
返回a;
返回b;
}
int find_subseq(char*a,char*b)
{
int i,j,la=strlen(a),lb=strlen(b);
int*arr=(int*)malloc(((la+1)*(lb+1)*sizeof(char));
对于(i=0;i
其次,在find_subseq函数中,当为数组arr分配内存时,不能说int*array=malloc(…*sizeof(char));它必须是
int*array=malloc(…*sizeof(int))

第三,在find_subseq函数中的for循环中,索引超出范围。这是一个巨大的问题,因为您将尝试访问不在该特定数组范围内的数组成员。这会导致程序中出现未定义的行为

for(j = 0; j < lb+1; j++)
(j=0;j
在上一次迭代中,当j=lb时,您将尝试访问数组[lb],但索引从0变为lb-1


如果你解释一下你想在程序中做什么,我可能会帮你更多。

你的格式字符串正在扫描单个字符,但你将其视为字符串。提示:崩溃的程序几乎总是有错误,因此标题假设是无效的。如果你不知道(根据这段代码判断,你不知道),那就不相关了
strlen
不是免费的。每个调用从开始到结束都扫描输入字符串。代码崩溃,但没有检查
malloc()
结果,也没有检查
scanf()
results。似乎第一步应该是添加代码检查这些内容——即使这与问题无关,也很好的防御性编程来捕捉简单的内容。
int*arr=(int*)malloc(((strlen(a)+1)*(strlen(b)+1)*sizeof(char)))
看起来非常错误。为什么
int
char
?尝试
int*arr=calloc(sizeof*arr,(strlen(a)+1)*(strlen(b)+1));
我在ideone上编译…成功了,但是hackerank中的编译器说了这一点…使用
sizeof*arr
而不是
sizeof(int)
避免了编码错误,更易于检查和维护。回答很好,但有一个例外:“这将导致内存碎片,这对您的程序非常有害”这恐怕是胡说八道。对于一个编程项目来说,在退出之前忽略释放所有内存可能是个问题,这取决于项目的需要,但它不会导致任何“碎片化”(或者,就此而言,丢失)关于内存。我们可以说这是一个潜在的问题,但我们不应该说这是一个自动的坏问题。我在大学里的所有老师都说,你们必须释放分配的内存,因为这样对程序来说是非常糟糕的(对于程序中每一个分配的变量,你们并没有使用free,你们在测试中得到-2分).我原以为这会造成内存碎片…现在,在你说了这些之后,我会读到的。谢谢you@AleksaJovanovic我了解你们的老师。这是一个有很多相互矛盾意见的问题。请参阅。@AleksaJovanovic此程序返回两个字符串之间最长公共子序列的长度。(我猜索引范围没有超出范围,因为数组不是n*m,而是(n+1)*(m+1)。
//this is wrong
int *arr = (int *)malloc(((la+1)*(lb+1)*sizeof(char)));
//this is right
int *arr = (int *)malloc(((la+1)*(lb+1)*sizeof(int)));
for(j = 0; j < lb+1; j++)