Ahmed Safwat
Ahmed Safwat

Reputation: 31

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 ! 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)

the problem is exactly in the function char* timeConversion(char* s)

in this line of code b = strcmp(ampm,"PM");

it always gives me error which i can not understand

"ERROR: invalid read of size 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;
}

Upvotes: 1

Views: 1149

Answers (2)

kiran Biradar
kiran Biradar

Reputation: 12742

There are 4 issues in your code.

  1. You are not allocating memory to NULL char for apmp

    char *ampm = (char*)calloc(3,sizeof(char)) ;

  2. You are receiving double pointer for swap function and passing char value

    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));

  3. You are not allocating memory to sub pointer

    char* sub = malloc (9 * sizeof(char));

  4. You are not deallocating memory for hh, sub and ampm.

    free (hh); free(ampm); free(sub);

Upvotes: 2

Benjamin Maurer
Benjamin Maurer

Reputation: 3753

Like the commenters mentioned, it seems like you are missing the NULL-termination, e.g.:

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.

Upvotes: 3

Related Questions