這個報錯是在將output_s這個地方使用malloc動態申請空間之后出現的,網上搜了相似錯誤看了也沒有找出問題來。
跪求告訴我是哪里錯了

char * longestPalindrome(char * s){
int head, tail;
int head_position,tail_position;
int output_head_position = 0, output_tail_position = 0;
int max_length = 0;
int length;
int i, j;
int x, y;
int k, l;
int size = strlen(s);
char *output_s = (char *)malloc(sizeof(char) * size);
for (i = 0; i < size; i++)
{
head = s[i];
head_position = i;
for (j = size - 1; j > i; j--)
{
if (head == s[j]) {
tail = s[j];
length = 2;
tail_position = j;
for (x = i + 1, y = j - 1; x <= y; x++, y--)
{
if (s[x] == s[y] && x != y) {
length += 2;
} else if (x == y) {
length += 1;
} else if (s[x] != s[y]){
length = 0;
break;
}
}
if (length > max_length) {
max_length = length;
output_head_position = head_position;
output_tail_position = tail_position;
}
}
}
}
for (k = output_head_position, l = 0; k <= output_tail_position; k++, l++)
{
output_s[l] = s[k];
}
return output_s;
}報錯資訊如下:
=================================================================
==29==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000003a at pc 0x00000040217c bp 0x7ffc39ee0860 sp 0x7ffc39ee0858
READ of size 1 at 0x60200000003a thread T0
#3 0x7fd69ad252e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
0x60200000003a is located 0 bytes to the right of 10-byte region [0x602000000030,0x60200000003a)
allocated by thread T0 here:
#0 0x7fd69c5d42b0 in malloc (/usr/local/lib64/libasan.so.5+0xe82b0)
#3 0x7fd69ad252e0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202e0)
Shadow bytes around the buggy address:
0x0c047fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c047fff8000: fa fa 06 fa fa fa 00[02]fa fa fa fa fa fa fa fa
0x0c047fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==29==ABORTING
uj5u.com熱心網友回復:
for (x = i + 1, y = j - 1; x <= y; x++, y--)
16 {
17 if (s[x] == s[y] && x != y) {
18 length += 2;
19 } else if (x == y) {
20 length += 1;
21 } else if (s[x] != s[y]){
22 length = 0;
23 break;
24 }
25 }
樓主有沒有考慮到,當外層回圈i=size-1時,x = i+1,就是x=size已經越界了吧
uj5u.com熱心網友回復:
另外,x++還繼續增長
uj5u.com熱心網友回復:
heep是堆,越界了,超限uj5u.com熱心網友回復:
這個地方在第for里面是有個x<=y的判斷條件的,當i=size-1的時候,x=size,j=size-1,y=size-2,x>y,后面的代碼應該會跳過執行,為什么還是會出現這種錯誤啊
uj5u.com熱心網友回復:
我好像找到問題在哪里了for (k = output_head_position, l = 0; k <= output_tail_position; k++, l++)
{
output_s[l] = s[k];
}
這個地方中的out_put_s是由
char *output_s = (char *)malloc(sizeof(char) * (size));
動態分配定義的,在輸入了最后一個字符到output_s變數后,由于沒有字串的結束標志,所以在leetcode提交后會繼續向最后有一個字符之后遍歷,所以出現了越界問題,但是當我將生成的空間多預留一位,并且手動加入'\0'標志后,輸出結果是這樣的

請問該如何解決啊
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/133586.html
標籤:C語言
上一篇:代碼出現亂碼恢復
