Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
该代码可以正常工作,并产生所需的输出,但最终导致该窗口弹出。
![c - 程序没有错误,但仍然崩溃-LMLPHP c - 程序没有错误,但仍然崩溃-LMLPHP]()
可以看出,未提及任何错误或警告。我想纠正这一点。
它绝对不会陷入循环,因为它正确地返回了所需的值。这个问题背后的原因可能是什么?请帮忙。
这是代码:
后来我检查了另一个编译器,它给出了这个错误:
这是什么意思?这是崩溃的原因吗?
第三,在find_subseq函数的for循环中,索引超出范围。这是一个很大的问题,因为您将尝试访问不在该特定数组范围内的数组成员。这会在程序中导致未定义的行为。
当在上一次迭代中j = lb时,您将尝试访问array [lb],但是索引从0到lb-1
也许如果您解释您打算在程序中做什么,我可以为您提供更多帮助。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
2年前关闭。
该代码可以正常工作,并产生所需的输出,但最终导致该窗口弹出。
可以看出,未提及任何错误或警告。我想纠正这一点。
它绝对不会陷入循环,因为它正确地返回了所需的值。这个问题背后的原因可能是什么?请帮忙。
这是代码:
#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;
}
后来我检查了另一个编译器,它给出了这个错误:
solution: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
GDB trace:
Reading symbols from solution...done.
[New LWP 10243]
Core was generated by `solution'.
Program terminated with signal SIGABRT, Aborted.
#0 0x00007f622e4b2428 in __GI_raise (sig=sig@entry=6)
at ../sysdeps/unix/sysv/linux/raise.c:54
#0 0x00007f622e4b2428 in __GI_raise (sig=sig@entry=6)
at ../sysdeps/unix/sysv/linux/raise.c:54
#1 0x00007f622e4b402a in __GI_abort () at abort.c:89
#2 0x00007f622e4fa2e8 in __malloc_assert (
assertion=assertion@entry=0x7f622e60e190 "(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)",
file=file@entry=0x7f622e60abc5 "malloc.c", line=line@entry=2394,
function=function@entry=0x7f622e60e9d8 <__func__.11509> "sysmalloc")
at malloc.c:301
#3 0x00007f622e4fe426 in sysmalloc (nb=nb@entry=4112,
av=av@entry=0x7f622e841b20 <main_arena>) at malloc.c:2391
#4 0x00007f622e4ff743 in _int_malloc (
av=av@entry=0x7f622e841b20 <main_arena>, bytes=bytes@entry=4096)
at malloc.c:3827
#5 0x00007f622e501184 in __GI___libc_malloc (bytes=bytes@entry=4096)
at malloc.c:2913
#6 0x00007f622e4ea1d5 in __GI__IO_file_doallocate (
fp=0x7f622e842620 <_IO_2_1_stdout_>) at filedoalloc.c:127
#7 0x00007f622e4f8594 in __GI__IO_doallocbuf (
fp=fp@entry=0x7f622e842620 <_IO_2_1_stdout_>) at genops.c:398
#8 0x00007f622e4f78f8 in _IO_new_file_overflow (
f=0x7f622e842620 <_IO_2_1_stdout_>, ch=-1) at fileops.c:820
#9 0x00007f622e4f628d in _IO_new_file_xsputn (
f=0x7f622e842620 <_IO_2_1_stdout_>, data=0x7ffe22f98f77, n=1)
at fileops.c:1331
#10 0x00007f622e4cae00 in _IO_vfprintf_internal (
s=0x7f622e842620 <_IO_2_1_stdout_>, format=<optimized out>,
format@entry=0x4008dd "%zu", ap=ap@entry=0x7ffe22f98fc8)
at vfprintf.c:1631
#11 0x00007f622e5939ef in ___printf_chk (flag=flag@entry=1,
format=format@entry=0x4008dd "%zu") at printf_chk.c:35
#12 0x000000000040062b in printf (__fmt=0x4008dd "%zu")
at /usr/include/x86_64-linux-gnu/bits/stdio2.h:104
#13 main () at solution.c:56
这是什么意思?这是崩溃的原因吗?
最佳答案
第一件事总是free()分配的内存。在main()函数中,您没有释放数组a和数组b。这不是一个错误,但是这样会使您的程序留下它是不好的
其次,在find_subseq函数中,当您为数组arr分配内存时,您不能说int * array = malloc(... * sizeof(char));一定是
int * array = malloc(... * sizeof(int))
//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)));
第三,在find_subseq函数的for循环中,索引超出范围。这是一个很大的问题,因为您将尝试访问不在该特定数组范围内的数组成员。这会在程序中导致未定义的行为。
for(j = 0; j < lb+1; j++)
当在上一次迭代中j = lb时,您将尝试访问array [lb],但是索引从0到lb-1
也许如果您解释您打算在程序中做什么,我可以为您提供更多帮助。
关于c - 程序没有错误,但仍然崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50295854/