简体   繁体   中英

using realloc() to change the malloc() size outputs garbage value (C language)

WHAT THE PROGRAM DO:

I am making a program that displays how many times a number has been entered by the user. It will stop asking value if a number less than one has been entered.

SCREENSHOT AND EXAMPLE OF THE PROGRAM

I was able to create the program by initializing the value of the array "count" to 100. SCREENSHOT OF WHAT I DID .

The issue with this program, is that it will only accept values until 100. It will not accept values more than 100. This is a screenshot if more than a hundred value has been entered: SCREENSHOT OF MORE THAN 100

THE PROBLEM

This is where I want realloc() to come in. I want the to change the malloc() size depending on the highest entered value so it will be more flexible using realloc(). SCREENSHOT OF WHAT I CHANGED IN THE PROGRAM TO USE REALLOC()

However, doing so destroys the program. SCREENSHOT OF THE NEW OUTPUT OF THE PROGRAM

Please help me.

MY PROGRAM

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

main()
{
    //DECLARATION OF VARIABLES
    int i, j, k, highestValue=1, size=1;
    int* input = (int*)calloc(size, sizeof(int));
    int* count = (int*)calloc(highestValue, sizeof(int));

    bool iCondition = true;

    //USER INPUT
    for (i=0; iCondition==true; i++)
    {
        //GETS USER INPUT
        printf("Enter a number: ");
        scanf("%d", &input[i]);

        //CHECKS IF THE NUMBER ENTERED IS A HIGH NUMBER 
        if (highestValue<input[i]){
            highestValue = input[i];
            count = realloc(count, highestValue * sizeof(int));
        }


        //CHECKS HOW MANY TIMES THE NUMBER HAS BEEN ENTERED
        bool jCondition = true;
        for(j=0; jCondition==true; j++)
        {       
            if (input[i] == j){
                count[j-1]++;
                jCondition=false;
            }       
        }

        //ENDS THE LOOP IF THE ENTERED NUMBER IS LESS THAN 1
        if(input[i] < 1)
            iCondition = false;

        //IF NOT, THIS WILL REALLOCATE/CHANGE ARRAY SIZE BY ADDING +1!!
        else{
            size++;
            input = realloc(input, size * sizeof(int));
        }
    }

    //PRINTS OUTPUT | USES THE HIGHESTVALUE AS THE CONDITIONAL EXPRESSION FOR FLEXIBILITY
    for (i=0; i<=highestValue; i++)
    {   
        //PRINTS ALL NUMBER THAT IS NOT EQUAL TO ZERO(0)
        if (count[i] != 0)
            printf("\n %d was entered %d time/s ", i+1, count[i]);
    }   
    getch();
}

When you use realloc , the elements after the end of the old array are unitialized. Thus, your value can be anybody including garbage.

Before using the newly created elements you should initialize them using the standard means (using a loop, using memset )...

Edit: Since you only allocate one extra element with realloc, you can initialize it directly with input[size-1] = '\\0' . Note that realloc has a non-trivial overhead so it is generally used to allocate multiple element at a time in real-life use cases.

Also, as mentionned by @KamikCuk, you should post directly text and not screenshots of text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM