动态内存分配时的垃圾值

garbage values while dynamic memory allocation

我的动态分配数组的代码,即使两个数组 patterntext 的输入方法相同 text 输出不同的值,任何人都可以解决这个问题吗?

#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

  1. 您应该检查 mallocscanf 的 return 值。
  2. Scanf 没有 return 扫描的元素。请检查 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);
}

包括 ctype.h 你可以使用库函数 int isdigit(char c) 接受输入 char 并告诉你它是否是 0 到 9 之间的数字。