Thread: Run-Time Check Failure #2 - Stack around the variable 'wordlength' was corrupted.

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    Run-Time Check Failure #2 - Stack around the variable 'wordlength' was corrupted.

    Since this is my first post in this forum, I start with "HEY GUYS!".

    I've tried to find a solution to my problem, but the only thing I found out was that it has something to do with a wrong value assigned to wordlenght. I've looked through the code but can't find the problem. Also it is to say that the program works just fine but after it's finished it gives me the error message seen in the thread title.

    It would be awesome if someone could take a look at the code and explain to me what's wrong or why this message comes up.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int		i=0,					
    			j=0,					
    			l=0,					
    			wordlength[60];			
    
    	char	inputstring[81];		
    
    	for(l=0; wordlength[l]<=60; l++)
    	{
    		wordlength[l]=0;
    	}
    
    
    	printf("Enter a sentence: ");
    	fgets(inputstring, 81, stdin);											
    
    	for(i=0; inputstring[i] != '.'; i++)
    	{	
    		if(inputstring[i] != ' ')
    		{
    			if(inputstring[i] >= 'a' && inputstring[i] <='z' || inputstring[i] >= 'A' && inputstring[i] <= 'Z')
    			{
    				j++;
    			}
    			else
    			{
    				printf("\nNOT VALID");
    				break;
    			}
    		}
    		else
    		{	
    			wordlength[j]=wordlength[j]+1;
    			j=0;
    		}
    
    		if(inputstring[i+1]=='.')
    		{
    			wordlength[j]=wordlength[j]+1;
    		}
    	}
    
    	for(l=0; wordlength[l]<=60; l++)
    	{
    		if(wordlength[l]!=0)
    		{
    			printf("Length %d: %d\n", l, wordlength[l]);
    		}
    	}
    }
    Thanks and sorry for my bad english.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    First, If you have an array of N elements, valid indices are 0-N-1 inclusive.


    Code:
    if(inputstring[i] >= 'a' && inputstring[i] <='z' || inputstring[i] >= 'A' && inputstring[i] <= 'Z')
    Why not use isalpha() from ctype.h?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    To be honest, I did not even know that there was a function called isalpha(), thanks for that.
    I know that if an array has for instance 80 elements the valid indices are 0-79 or if it's a string 0-78 because the last index has '\0' written into it.
    But I can't see where I use a indexvalue which is bigger than my number of elements.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    But I can't see where I use a indexvalue which is bigger than my number of elements.
    This might hit the spot.

    Code:
    	for(l=0; wordlength[l]<=60; l++)
    	{
    		wordlength[l]=0;
    	}
    And this:

    Code:
    	for(l=0; wordlength[l]<=60; l++)
    	{
    		if(wordlength[l]!=0)
    		{
    			printf("Length %d: %d\n", l, wordlength[l]);
    		}
    	}
    Your maximum wordlength is 59 ( 0 - 59 just like Bayint Naung said). YOU are trying to access 0 - 60 in the above code. You should not be using the "<=" operator, (Hint greater or equal).

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Oh don't know how I didn't see that, thanks.
    I'm not really sure if the >= operator would serve my needs, since I need to fill the array with a zero as long as "i" is smaller as 59. Sorry if I don't get it, I'm pretty new to programming.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    This is all wrong:
    Code:
    for(l=0; wordlength[l]<=60; l++)
    	{
    		wordlength[l]=0;
    	}
    Should be:
    Code:
    for(l=0; l < 60; l++)
    	{
    		wordlength[l]=0;
    	}
    And why are you mixing up i and l ?

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    Just changed that before I saw your post, still thank you.
    You're right, I shouldn't mix i and l, it's a little confusing.
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  2. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  3. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM