Search code examples
cdynamic-memory-allocation

garbage values while dynamic memory allocation


My code for dynamically allocating arrays, even though the Input methods for both arrays pattern and text are same text outputs different values, can anyone solve this issue?

#include<stdio.h>
#include<stdlib.h>

int main() {
    int length=5;
    int * pattern = malloc(length * sizeof(int));
    int * text = malloc(length * sizeof(int));  
    int pattern_size=0;
    int text_size=0;
    printf("Enter Pattern:");

    char c; 
    while(c != '$' && scanf("%c",&c) != '\n'){ 
        if(pattern_size >= length)
            pattern = realloc(pattern, (length += 10) * sizeof(int));
        if(c!=',') pattern[pattern_size] = atoi(&c)+pattern[pattern_size]*10;
        else if(c==',') {
            pattern_size++;
        }
        
    }

    printf("\nPlease enter the replacement text:");
    // get_array(text,&text_size,length);

    char d; 
    while(d != '$' && scanf("%c",&d) != '\n'){ 
        if(text_size >= length)
            text = realloc(text, (length += 10) * sizeof(int));
        if(d!=',') text[text_size] = atoi(&d)+text[text_size]*10;
        else if(d==',') {
            text_size++;
        }
        
    }

    for(int i=0;i<pattern_size; i++){
        printf("%d ",pattern[i]);
    }
    printf("\n");
    for(int i=0;i<text_size; i++){
        printf("%d ",text[i]);
    }
    printf("\n");
    return 0;
}
Input
Enter Pattern:1,2,3,4,5,6,7,8,9,0,$
Please enter the replacement text:1,2,3,4,5,6,7,8,9,0,$
OUTPUT
1 2 3 4 5 6 7 8 9 0
1 2 3 4 5 6 10417 8 540155953 540287027


Solution

    1. You should ever check the return value of malloc and scanf.
    2. Scanf does not return the element scanned. Please check man 3 scanf

    On success, these functions return the number of input items success‐ fully matched and assigned; this can be fewer than provided for, or even zero, in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set to indicate the error.

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
        int length = 1;
        int *pattern = calloc(length, sizeof(int));
        if (pattern == NULL) {
            fprintf(stderr, "Unable to find free memory\n");
            exit(EXIT_FAILURE);
        }
        int* text = calloc(length, sizeof(int)); 
        if (text == NULL) {
            fprintf(stderr, "Unable to find free memory\n");
            exit(EXIT_FAILURE);
        }
        int pattern_size = 0;
        int text_size = 0;
        printf("Enter Pattern:\n");
        char c = ' '; 
        while (c != '$') { 
            if (scanf("%c", &c) != 1) {
                fprintf(stderr, "Error in scanf\n");
                exit(EXIT_FAILURE);
            }
            if (isdigit(c) != 0) {
                pattern[pattern_size] = c - 48;
                pattern_size++;
                pattern = realloc(pattern, (pattern_size + 1) * sizeof(int));
            }
        }
        printf("\nPlease enter the replacement text:\n");
        // get_array(text,&text_size,length);
        char d = ' '; 
        while (d != '$') { 
            if (scanf("%c", &d) != 1) {
                fprintf(stderr, "Error in scanf\n");
                exit(EXIT_FAILURE);
            }
            if (isdigit(d) != 0) {
                text[text_size] = d - 48;
                text_size++;
                text = realloc(text, (text_size + 1) * sizeof(int));
            }
        }
        fprintf(stdout, "\nOUTPUT:\n");
        for (int i = 0; i < pattern_size; i++) 
            printf("%d ", pattern[i]);
        printf("\n");
        for (int i = 0; i < text_size; i++)
            printf("%d ", text[i]);
        printf("\n");
        return 0;
        free(pattern);
        free(text);
    }
    

    Including ctype.h you canuse the library function int isdigit(char c) that take in input a char and tells you if it is a number between 0 and 9.