首页 > 解决方案 > 动态内存分配时的垃圾值

问题描述

我的动态分配数组的代码,即使两个数组的输入方法都pattern输出text不同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

标签: cdynamic-memory-allocation

解决方案


  1. 您应该检查mallocand的返回值scanf
  2. Scanf不返回扫描的元素。请检查man 3 scanf

成功时,这些函数返回成功匹配和分配的输入项的数量;如果早期匹配失败,这可能会小于规定的值,甚至为零。如果在第一次成功转换或匹配失败发生之前到达输入结尾,则返回值 EOF。如果发生读取错误,也会返回 EOF,在这种情况下会设置流的错误指示符(请参阅 ferror(3)),并设置 errno 以指示错误。

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

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


推荐阅读