Program crashes after input a String using Malloc

c string pointers

88 观看

2回复

9 作者的声誉

The program crashed after entering a long string. I think the program cannot handle a large amount of string entered by the user. Can you help me whats wrong on my code?

a simple C-program that counts the vowel and consonants in a string using pointers.

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

int main()
{
    char * sEnteredString= NULL;
    int i;
    int sizeString;
    int vowel=0;
    int consonants=0;

    printf("A program that counts the number of vowels and consonants in a string\n");
    printf("Enter a string:\t");
    sEnteredString = (char*)malloc(sizeof(sEnteredString));
    scanf(" %s",sEnteredString);

    if (sEnteredString==NULL)
    {
        printf("OUT OF MEMORY\n");
    }
    else
    {
        sizeString=strlen(sEnteredString);

        for (i=0; i<sizeString;i++)
        {
            if((sEnteredString[i] == 'A') || \
               (sEnteredString[i] == 'a') || \
               (sEnteredString[i] == 'E') || \
               (sEnteredString[i] == 'e') || \
               (sEnteredString[i] == 'I') || \
               (sEnteredString[i] == 'i') || \
               (sEnteredString[i] == 'O') || \
               (sEnteredString[i] == 'o') || \
               (sEnteredString[i] == 'U') || \
               (sEnteredString[i] == 'u'))
            {
                vowel ++;
            }
            else
            {
                consonants ++;
            }
        }
            printf("Entered vowel: %d \nEntered constants: %d\n", vowel, consonants);
    }

    free(sEnteredString);
    return 0;
}
作者: JonnahMae 的来源 发布者: 2017 年 12 月 27 日

回应 2


1

112247 作者的声誉

why are you (wrongly) allocating dynamic memory for this:

sEnteredString = (char*)malloc(sizeof(sEnteredString));

where sizeof(sEnteredString) is the size of the char pointer (which is usually 4 or 8) ? so too short when entering a string like "hey! waffles!".

when you can do:

char sEnteredString[100];

then

scanf("%99s",sEnteredString);

so you auto allocate your string and there's a safety in case someone enters a too big string.

Important: don't free(sEnteredString) in the end

Aside: use toupper along with a switch/case statement to avoid all those ifs. And remember to count consonants, not chars that aren't vowels (like spaces, punctuation, ...)

作者: Jean-François Fabre 发布者: 2017 年 12 月 27 日

1

27632 作者的声誉

The major problem is that you are allocating memory for only 8 chars or 4 chars based on the size of pointer. (varies due to 32-64 bit systems). It is unlikely that your string would hold in 4 or 8 char's with the NUL terminating character. And then you try to read string larger than this , you have illegal memory access...accessing something you didn't allocate. You clearly have undefined behavior.(In your case program crashed).

Second problem is your checking of malloc shouldn't it be before you use it? Yes..but you didn't follow that.

sEnteredString = malloc(50);
if( sEnteredString == NULL){

 /* error */
}
else{
   if( scanf("49%s",sEnteredString)!= 1){
     // error
   }
   // at this point you are happy that sEnteredString points to
   // a nul terminated char array 
   // strlen can be used safely here.
   size_t sizestring  = strlen(sEnteredString);

}
...
free(sEnteredString);

Yes another thing is you shouldn't cast the return value of malloc - it's redundant.

Also note I have used size_t to hold return value of strlen.

And yes easy way to check what you did will be

   if( strchr("aAeEiIoOuU",sEnteredString[i]) != NULL ){
     // you got a vowel
   }

Also keep in mind one thing strlen is supposed to get a pointer to a null terminated char array. If you don't provide that you will have undefined behavior.

Code

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

int main(void)
{
    char * s;

    s = malloc(50);  
    if (s == NULL)
    {
        perror("Malloc failed\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("A program that counts the number of vowels and consonants in a string\n");
        printf("Enter a string:\t");

        if(scanf("%49s",s)!= 1){
            fprintf(stderr, "%s\n", "Error in input");
            free(s);
            exit(EXIT_FAILURE);
        }
        size_t sz = strlen(s);
        size_t vowel = 0,consonants = 0;
        for (size_t i=0; i<sz; i++)
        {
            strchr("aAeEiIoOuU",s[i]) ? vowel++ : consonants++;
        }
        printf("Entered vowel: %zu \nEntered constants: %zu\n", vowel, consonants);
    }
    free(s);
    return EXIT_SUCCESS;
}
作者: user2736738 发布者: 2017 年 12 月 27 日
32x32