Memory Corruption

This is a discussion on Memory Corruption within the C Programming forums, part of the General Programming Boards category; Hey guys, I know I shouldn't be posting such a simple assignment problem on here, however, I have run in ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    Memory Corruption

    Hey guys,

    I know I shouldn't be posting such a simple assignment problem on here, however, I have run in to a rut and I can't seem to locate the error and I'm simply seeking a new set of eyes to see if you can spot it, it can't be difficult but I just can't seem to find it.

    Here is what I have:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	int letters[26]={0}, len, i=0, tlet;
    	char input[50], temp;
    
    	// gather input and adjust as appropriate
    	printf("Enter a line of text to be counted: ");
    	gets(input);
    	len=(int)strlen(input);
    
    	// scan through and count
    	for( i=0 ; i < len ; i++ ) {
    		temp=input[i];
    		tlet=temp-'a';
    
    		// ensure that the character is a lower case letter
    		if( (tlet > -1) && (tlet < 26) )
    			letters[tlet]++;
    	}
    
    	// display results (only results with values above 0)
    	for( i=0 ; i < 26 ; i++ ) {
    		if( letters[i] > 0 ) { printf("%c: %d\n", ('a'+i), letters[i]); }
    	}
    
    	return(0);
    }
    Now this is unforunately a rudamentory programming class, but university is making take this stuff all over again. What the problem with this is, after gaining the input and processing that input my program seems to corrupt the line... now the line we have to use for input is "This is sample text for demonstrating exercise (2). Only lower case counted." For some reason right around the "lower case" section of the sentence, the data (everytime it's run) gets corrupted and letters don't get counted. MSVC also gives me a debug error saying that Stack around the variable 'input' was corrupted.

    Any fresh eyes out there able to see why this is? As a side note, directly after gaining the input I output the text to the screen (during testing) and everytime it gets displayed properly. I can't seem to figure out why then, with the code above, the variable is suddenly becoming corrupted during run-time since I'm not actually doing anything with the input other then counting the number of lower case letters entered and outputting the number of each letter (for those who have counted values above 0). Also the lines using the 'temp' variable was adjusted to see if perhaps using the input[i] data directly was causing the corruption, but it doesn't seem to be the case.

    Any fresh eyes would be greatly appreciated on this, it's been giving me a headache. lol.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    This is a lot longer than 50 characters. (The first sentence is about 50, I think.)
    Code:
    This is sample text for demonstrating exercise (2). Only lower case counted.
    However, you only have space for 49 characters plus a NULL.
    Code:
    	char input[50];
    	/* ... */
    	gets(input);
    gets() can easily cause buffer overruns of this sort, which is why it's highly un-recommended. http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Try using fgets() instead, with a larger buffer.

    As for why such strange things are happening? That's the nature of buffer overruns, and memory errors in general. It can work perfectly; a variable can suddenly change its value, seemingly randomly; you can get a segmentation fault. Who knows. More than once I've written a program which accidently rebooted my computer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Hahaha, omg... I really do need a break from school. I even was printing out the len value after gaining input and it told me 72 for the length, and I still didn't look at that!

    See knew I needed a fresh pair of eyes. Thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	int letters[26]={0}, len, i=0, tlet;
    I would recommend also not mixing uninitialzied and initialized variables on teh same line. That is just a style issue, but it make lines easier to read if you only do one or the other [and I prefer to only put one initialization on one line].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 05:04 AM
  2. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21