C - 大小为 1 的无效读取
C - invalid read of size 1
好吧,已经超过 15 个小时了,我仍然不知道发生了什么!
这是hackerrank中时间转换问题的代码,一个函数接受一个字符串(12小时AM/PM格式的时间)并将其转换为军事(24小时)时间(返回一个字符串)
问题出在函数char* timeConversion(char* s)
在这行代码中
b = strcmp(ampm,"PM");
它总是给我无法理解的错误
"ERROR: invalid read of size 1"
谁能帮帮我?!
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
/*
* Complete the timeConversion function below.
*/
/*
* Please either make the string static or allocate on the heap. For example,
* static char str[] = "hello world";
* return str;
*
* OR
*
* char* str = "hello world";
* return str;
*
*/
/* Swaps strings by swapping pointers */
void swap(char **str1_ptr, char **str2_ptr)
{
char *temp = *str1_ptr;
*str1_ptr = *str2_ptr;
*str2_ptr = temp;
}
void reverse(char str[], int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(*(str+start), *(str+end));
start++;
end--;
}
}
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '[=10=]';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '[=10=]'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
char* timeConversion(char* s) {
/*
* Write your code here.
*/
char *result = (char*)calloc(8,sizeof(char)) ;
char *ampm = (char*)calloc(2,sizeof(char)) ;
char *hh = (char*)calloc(2,sizeof(char)) ;
int a = 0, b = 0 ,c = 0,i;
long int dec = 0;
int len = strlen(s);
// substring hh:mm:ssAM
while ( c < 2) // 2 : LENGTH
{
ampm[c] = s[9+c-1]; // 9 : position
hh[c] = s[1+c-1]; // 1 : position
c++ ;
}
// string to int
//len = strlen(ampm);
for(i = 0; i < 2 ; i++)
{
dec = dec * 10 + (hh[i] - '0');
}
b = strcmp(ampm,"PM");
a = strcmp(ampm,"AM");
printf("%d\n",a);
printf("%d\n",b);
// processing
if (!strcmp(ampm,"AM") && dec==12) dec = 0;
if (!strcmp(ampm,"PM") && dec!=12) dec += 12;
//if (strcmp(s[9],'A') && dec==12) dec = 0;
//if (strcmp(s[9],'P') && dec!=12) dec += 12;
// convert int back to string
char* hhh = itoa(dec, hh, 10);
//dec = atol(hh);
// hh = itoa(dec,10);
// snprintf(result,9,"%d", dec);
//printf("%s\n",hh);
c = 0;
char* sub;
while (c < 9)
{
sub[c] = s[3+c-1];
c++ ;
}
strcat(result,hhh);
strcat(result,sub);
return result;
}
int main()
{
char *s = "07:05:45PM";
char* result = timeConversion(s);
printf("%s\n", result);
return 0;
}
就像提到的评论者一样,您似乎缺少 NULL 终止符,例如:
char *ampm = (char*)calloc(2,sizeof(char)) ;
两个字符 ('am'/'pm') 加上 NULL 终止符将是 3 个字符 ,而不是 2 个。
您必须确保所有字符串的大小都是 len + 1 并且正确地以“\0”结尾。
您的代码中有 4 个问题。
您没有为 apmp
的 NULL
字符分配内存
char *ampm = (char*)calloc(3,sizeof(char)) ;
您正在接收 swap
函数的双指针并传递 char
值
void swap(char **str1_ptr, char **str2_ptr)
应该是
void swap(char *str1_ptr, char *str2_ptr)
然后你像下面这样调用交换函数
swap((str+start), (str+end));
您没有为 sub
指针分配内存
char* sub = malloc (9 * sizeof(char));
您没有为 hh
、sub
和 ampm
释放内存。
free (hh); free(ampm); free(sub);
好吧,已经超过 15 个小时了,我仍然不知道发生了什么! 这是hackerrank中时间转换问题的代码,一个函数接受一个字符串(12小时AM/PM格式的时间)并将其转换为军事(24小时)时间(返回一个字符串)
问题出在函数char* timeConversion(char* s)
在这行代码中
b = strcmp(ampm,"PM");
它总是给我无法理解的错误
"ERROR: invalid read of size 1"
谁能帮帮我?!
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* readline();
/*
* Complete the timeConversion function below.
*/
/*
* Please either make the string static or allocate on the heap. For example,
* static char str[] = "hello world";
* return str;
*
* OR
*
* char* str = "hello world";
* return str;
*
*/
/* Swaps strings by swapping pointers */
void swap(char **str1_ptr, char **str2_ptr)
{
char *temp = *str1_ptr;
*str1_ptr = *str2_ptr;
*str2_ptr = temp;
}
void reverse(char str[], int length)
{
int start = 0;
int end = length -1;
while (start < end)
{
swap(*(str+start), *(str+end));
start++;
end--;
}
}
// Implementation of itoa()
char* itoa(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '[=10=]';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num/base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '[=10=]'; // Append string terminator
// Reverse the string
reverse(str, i);
return str;
}
char* timeConversion(char* s) {
/*
* Write your code here.
*/
char *result = (char*)calloc(8,sizeof(char)) ;
char *ampm = (char*)calloc(2,sizeof(char)) ;
char *hh = (char*)calloc(2,sizeof(char)) ;
int a = 0, b = 0 ,c = 0,i;
long int dec = 0;
int len = strlen(s);
// substring hh:mm:ssAM
while ( c < 2) // 2 : LENGTH
{
ampm[c] = s[9+c-1]; // 9 : position
hh[c] = s[1+c-1]; // 1 : position
c++ ;
}
// string to int
//len = strlen(ampm);
for(i = 0; i < 2 ; i++)
{
dec = dec * 10 + (hh[i] - '0');
}
b = strcmp(ampm,"PM");
a = strcmp(ampm,"AM");
printf("%d\n",a);
printf("%d\n",b);
// processing
if (!strcmp(ampm,"AM") && dec==12) dec = 0;
if (!strcmp(ampm,"PM") && dec!=12) dec += 12;
//if (strcmp(s[9],'A') && dec==12) dec = 0;
//if (strcmp(s[9],'P') && dec!=12) dec += 12;
// convert int back to string
char* hhh = itoa(dec, hh, 10);
//dec = atol(hh);
// hh = itoa(dec,10);
// snprintf(result,9,"%d", dec);
//printf("%s\n",hh);
c = 0;
char* sub;
while (c < 9)
{
sub[c] = s[3+c-1];
c++ ;
}
strcat(result,hhh);
strcat(result,sub);
return result;
}
int main()
{
char *s = "07:05:45PM";
char* result = timeConversion(s);
printf("%s\n", result);
return 0;
}
就像提到的评论者一样,您似乎缺少 NULL 终止符,例如:
char *ampm = (char*)calloc(2,sizeof(char)) ;
两个字符 ('am'/'pm') 加上 NULL 终止符将是 3 个字符 ,而不是 2 个。 您必须确保所有字符串的大小都是 len + 1 并且正确地以“\0”结尾。
您的代码中有 4 个问题。
您没有为
的apmp
NULL
字符分配内存char *ampm = (char*)calloc(3,sizeof(char)) ;
您正在接收
swap
函数的双指针并传递char
值void swap(char **str1_ptr, char **str2_ptr)
应该是
void swap(char *str1_ptr, char *str2_ptr)
然后你像下面这样调用交换函数
swap((str+start), (str+end));
您没有为
sub
指针分配内存char* sub = malloc (9 * sizeof(char));
您没有为
hh
、sub
和ampm
释放内存。free (hh); free(ampm); free(sub);