Thread: while loop stack bashing detected

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    while loop stack bashing detected

    Hi all, i have written a program to demonstrate while loops and arrays however when i exit the program by pressing n i get the following error message.
    *** stack smashing detected ***: <unknown> terminated Aborted (core dumped)
    process returned 134 (0x86) etc

    this only happens when i try to quit the program after the first time round.

    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a=0;
        int i;
        char answer;
        char words[4];
    
        while (a != 2)
        {
            //check to see if first time run and ask the appropriate question
            if (a == 0)
            {
                printf("Would you like to enter a word y/n\n");
                scanf(" %c",&answer);
            }
            else if (a == 1)
            {
                printf("Would you like to enter another word y/n\n");
                scanf(" %c",&answer);
            }
            else
            {
                printf("Sorry I didnt understand that Please enter "y" to enter a word or "n" to quit\n");
                scanf(" %c",&answer);
            }
    
            //check what the answer was and run for loop if need be
            if (answer == 'y' || answer == 'Y')
            {
                printf("Please enter a five letter word\n");
                scanf(" %s",words);
                for (i = 0; i < 5; i++)
                {
                    printf("The letter in posistion %d is %c\n",i ,words[i]);
                }
                printf("the word entered was %s\n",words);
                a=1;
            }
            else if (answer == 'n' || answer == 'N')
            {
                printf("goodbye!!\n");
                a=2;
            }
            else // somthing else typed in // todo error trapping for more than one character entered
            {
                a=3;
            }
        }
    
        return 0;
    }
    many thanks
    Coop

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Code:
     char words[4];
    Is not large enough to hold a 5 letter word - You will need 6 chars (5 + an extra for the nul char

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by Click_here View Post
    Code:
     char words[4];
    Is not large enough to hold a 5 letter word - You will need 6 chars (5 + an extra for the nul char
    thanks for the reply is this the same for an array of integers ie to hold 5 integers i need myarray[5]

    many thanks
    coop

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    An array of int to hold 5 integers is indeed something like myarray[5]. Your problem is that you don't seem to understand the difference between an array of char and a C-string. A C-string requires a termination nul character so in order to hold a C-string with 5 characters you need an array of size 6 so that there is room for that termination character that the "%s" specifier will automatically adds to the characters entered. Also you should never use a function that will try to retrieve more characters than the size string, the scanf() function can accomplish this limiting by using the proper width specifier. This specifier must be at least one smaller than the size of the array. For example:

    Code:
    char mystring[6];  // A C-string large enough to hold 5 charcters.
    scanf("%5s", mystring); // Limit scanf() from overflowing the bounds of the array and save room for the end of string character.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by click_here
    You will need 6 chars (5 + an extra for the nul char
    For more info, see C Strings - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can it be detected if stack space is about to expire?
    By manasij7479 in forum C++ Programming
    Replies: 50
    Last Post: 12-03-2011, 03:04 PM
  2. Stack Smashing Detected
    By halexh in forum C Programming
    Replies: 13
    Last Post: 05-19-2010, 02:10 AM
  3. *** stack smashing detected ***
    By chakra in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 09:12 PM
  4. *** stack smashing detected ***
    By Martin_HS in forum C Programming
    Replies: 9
    Last Post: 05-29-2009, 04:01 AM
  5. help on *** stack smashing detected ***
    By jodelson in forum C Programming
    Replies: 8
    Last Post: 08-16-2007, 06:25 PM

Tags for this Thread