[英]C - invalid read of size 1
ok that's it, it's been more than 15 hours and i still can't figure out what's going on ! 好的,已经过去了15个多小时,我仍然不知道发生了什么! Here is a code for timeconversion problem in hackerrank, a function takes a string (time in 12--hour AM/PM format) and converts it to military (24-hour) time (returning a string)
这是hackerrank中时间转换问题的代码,一个函数接受一个字符串(时间为12小时AM / PM格式)并将其转换为军事(24小时)时间(返回一个字符串)
the problem is exactly in the function char* timeConversion(char* s)
问题恰好在函数
char* timeConversion(char* s)
in this line of code b = strcmp(ampm,"PM");
在这行代码中
b = strcmp(ampm,"PM");
it always gives me error which i can not understand 它总是给我我无法理解的错误
"ERROR: invalid read of size 1"
“错误:大小为1的读取无效”
can anyone help me ?! 谁能帮我 ?!
#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] = '\0';
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] = '\0'; // 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;
}
Like the commenters mentioned, it seems like you are missing the NULL-termination, eg: 就像提到的评论者一样,您似乎缺少NULL终止,例如:
char *ampm = (char*)calloc(2,sizeof(char)) ;
Two characters ('am'/'pm') plus the NULL-Termination would be 3 characters , not 2. You have to make sure that all your strings have a size of len + 1 and are properly '\\0' terminated. 两个字符('am'/'pm')加上NULL-Termination将是3个字符 ,而不是2 个字符 。您必须确保所有字符串的大小均为len +1,并且正确地以'\\ 0'结尾。
There are 4 issues in your code. 您的代码中有4个问题。
You are not allocating memory to NULL
char for apmp
您没有将内存分配给
apmp
NULL
char
char *ampm = (char*)calloc(3,sizeof(char)) ;
You are receiving double pointer for swap
function and passing char
value 您正在接收
swap
函数的双指针并传递char
值
void swap(char **str1_ptr, char **str2_ptr)
should be 应该
void swap(char *str1_ptr, char *str2_ptr)
and you call swap function like below 然后您调用交换函数,如下所示
swap((str+start), (str+end));
You are not allocating memory to sub
pointer 您没有将内存分配给
sub
指针
char* sub = malloc (9 * sizeof(char));
You are not deallocating memory for hh
, sub
and ampm
. 您没有为
hh
, sub
和ampm
分配内存。
free (hh); free(ampm); free(sub);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.