我试图解决来自leet代码的一个问题,称为push Dominoes.The代码在我的PC上工作,但是我在leetcode编译器中遇到了堆缓冲区溢出,我不明白为什么。问题的链接是- https://leetcode.com/problems/push-dominoes/,我的代码是-(编辑)
#include <stdio.h>
#include <malloc.h>
// to print array
void printArr(char *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%c ", arr[i]);
}
printf("---\n");
}
void printArrInt(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
char *pushDominoes(char *dominoes)
{
int i = 0;
/*
mkae two array - right push and left push
fill it with the sec measures
compare and put in the final one
*/
// finding length of the string
int len = 0;
while (dominoes[len] != '\0')
{
len++;
}
// making tree array
int *lftPush = (int *)malloc(sizeof(int) * len);
int *rightpush = (int *)malloc(sizeof(int) * len);
char *final = (char *)malloc((sizeof(char) * len) + 1);
// filling the right push
int lstRight = 0;
while (dominoes[i] != '\0')
{
// printf("checking for char - %c and lstright = %d\n",dominoes[i],lstRight);
if ((dominoes[i] == '.') && (lstRight == 0))
{
rightpush[i] = 0;
}
else if ((dominoes[i] == '.') && (lstRight != 0))
{
rightpush[i] = lstRight;
lstRight++;
}
else if (dominoes[i] == 'L')
{
lstRight = 0;
rightpush[i] = 0;
}
else if (dominoes[i] == 'R')
{
// printf("Check inside\n");
rightpush[i] = 0;
lstRight++;
}
else
{
printf("Didnt get what to do in right oush \n");
}
i++;
}
// filling the left push
int lstLeft = 0;
i = 0;
for (i = len - 1; i >= 0; i--)
{
// printf("checking for char - %c and lstLeft = %d\n", dominoes[i], lstLeft);
if ((dominoes[i] == '.') && (lstLeft == 0))
{
lftPush[i] = 0;
}
else if ((dominoes[i] == '.') && (lstLeft != 0))
{
lftPush[i] = lstLeft;
lstLeft++;
}
else if (dominoes[i] == 'L')
{
lstLeft++;
lftPush[i] = 0;
}
else if (dominoes[i] == 'R')
{
// printf("Check inside\n");
lftPush[i] = 0;
lstLeft = 0;
}
else
{
printf("Didnt get what to do in right oush for character = %d\n", i);
}
}
// comparing both the arrays
i = 0;
while (dominoes[i] != '\0')
{
if ((lftPush[i] == 0) && (rightpush[i] == 0))
{
final[i] = dominoes[i];
}
else if (lftPush[i]==0 && rightpush[i]!=0)
{
final[i] = 'R';
}
else if (lftPush[i]!=0 && rightpush[i]==0)
{
final[i] = 'L';
}
else if (lftPush[i] < rightpush[i])
{
final[i] = 'L';
}
else if (lftPush[i] > rightpush[i])
{
final[i] = 'R';
}
else if (lftPush[i] == rightpush[i])
{
final[i] = '.';
}
else
{
printf("Did'nt get what to do in right oush for character = %d\n", i);
}
i++;
}
// printArrInt(lftPush, len);
// printArr(final, len);
free(lftPush);
free(rightpush);
return final;
}
int main()
{
char *s = ".L.R...LR..L..";
// char *s = "RR.L";
printf("%s", pushDominoes(s));
// pushDominoes(s);
return 0;
}对不起,这么乱的代码和这么多语法上的错误评论,我试图理解我自己的一个长期的time.The堆缓冲区流错误是-
==31==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000002f at pc 0x556b2810f441 bp 0x7ffc128a0780 sp 0x7ffc128a0770
READ of size 1 at 0x60200000002f thread T0
#2 0x7efe1d03c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x60200000002f is located 1 bytes to the left of 5-byte region [0x602000000030,0x602000000035)
allocated by thread T0 here:
#0 0x7efe1dc81bc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8)
#3 0x7efe1d03c0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)我怎样才能从这样的错误中找出我的错呢?我知道这是一个很长的路要得到一个答案,但我是一个新手。提前感谢,为语法错误感到抱歉。
编辑-我以前发布过这个问题,但我正在用一种新的方法再次做这个问题。.That仍然有一个错误,这就是为什么我要重新发布
发布于 2022-09-27 19:18:23
只需终止最终字符并对代码进行一些最终更改即可。
#include <stdio.h>
#include <malloc.h>
// to print array
void printArr(char *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%c ", arr[i]);
}
printf("---\n");
}
void printArrInt(int *arr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
char *pushDominoes(char *dominoes)
{
int i = 0;
/*
mkae two array - right push and left push
fill it with the sec measures
compare and put in the final one
*/
// finding length of the string
int len = 0;
while (dominoes[len] != '\0')
{
len++;
}
// making tree array
int *lftPush = (int *)malloc(sizeof(int) * len);
int *rightpush = (int *)malloc(sizeof(int) * len);
char *final = (char *)malloc((sizeof(char) * len) + 1);
// filling the right push
int lstRight = 0;
while (dominoes[i] != '\0')
{
// printf("checking for char - %c and lstright = %d\n",dominoes[i],lstRight);
if ((dominoes[i] == '.') && (lstRight == 0))
{
rightpush[i] = 0;
}
else if ((dominoes[i] == '.') && (lstRight != 0))
{
rightpush[i] = lstRight;
lstRight++;
}
else if (dominoes[i] == 'L')
{
lstRight = 0;
rightpush[i] = 0;
}
else if (dominoes[i] == 'R')
{
// printf("Check inside\n");
rightpush[i] = 0;
lstRight=1;
}
else
{
printf("Didnt get what to do in right oush \n");
}
i++;
}
// filling the left push
int lstLeft = 0;
i = 0;
for (i = len - 1; i >= 0; i--)
{
// printf("checking for char - %c and lstLeft = %d\n", dominoes[i], lstLeft);
if ((dominoes[i] == '.') && (lstLeft == 0))
{
lftPush[i] = 0;
}
else if ((dominoes[i] == '.') && (lstLeft != 0))
{
lftPush[i] = lstLeft;
lstLeft++;
}
else if (dominoes[i] == 'L')
{
lstLeft=1;
lftPush[i] = 0;
}
else if (dominoes[i] == 'R')
{
// printf("Check inside\n");
lftPush[i] = 0;
lstLeft = 0;
}
else
{
printf("Didnt get what to do in right oush for character = %d\n", i);
}
}
// comparing both the arrays
i = 0;
while (dominoes[i] != '\0')
{
if ((lftPush[i] == 0) && (rightpush[i] == 0))
{
final[i] = dominoes[i];
}
else if (lftPush[i]==0 && rightpush[i]!=0)
{
final[i] = 'R';
}
else if (lftPush[i]!=0 && rightpush[i]==0)
{
final[i] = 'L';
}
else if (lftPush[i] < rightpush[i])
{
final[i] = 'L';
}
else if (lftPush[i] > rightpush[i])
{
final[i] = 'R';
}
else if (lftPush[i] == rightpush[i])
{
final[i] = '.';
}
else
{
printf("Did'nt get what to do in right oush for character = %d\n", i);
}
i++;
}
final[len]='\0';
printArrInt(lftPush, len);
printArrInt(rightpush, len);
// printArr(final, len);
free(lftPush);
free(rightpush);
return final;
}
int main()
{
// char *s = ".L.R...LR..L..";
char *s = "RR.L";
printf("%s", pushDominoes(s));
// pushDominoes(s);
return 0;
}https://stackoverflow.com/questions/73872045
复制相似问题