stack overflow....

This is a discussion on stack overflow.... within the C Programming forums, part of the General Programming Boards category; I have run into this peculiar problem of stack overflow and how do i deal with it. The thing is ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    stack overflow....

    I have run into this peculiar problem of stack overflow and how do i deal with it. The thing is that when i was trying to run the program it was giving that .exe has encountered a problem and will close and all other crap that do you want to tell microsoft about this. But then when i changed to debug mode it gave me the error "Stack Overflow". The program is as follows:

    [insert]
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    #define MAX_WORD_LENGTH 31
    #define MAX_WORDS 42000
    
    /* Function prototypes */
    int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag);
    //int qsort(char array[][MAX_WORD_LENGTH], int* wordCount);
    int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH]);
    void printTime(time_t  begin);
    void usage(char* processName);
    // enter any other function prototype you may wish
    
    int main(int argc, char* argv[])
    {
            time_t begin;   
    		char array[MAX_WORDS][MAX_WORD_LENGTH];
    		int wordCount = 0; 
                time(&begin);
    		
           	switch (argc)
    		{
    			case 3:
    			readFile(argv[1], &wordCount, array,0);
    			writeToFile(argv[2], wordCount, array);
    			break;
    		case 4:
    		{
    			if (strcmp(argv[1], "-s") == 0)
    			  {
    			   readFile(argv[2], &wordCount, array,1);
         		   writeToFile(argv[3], wordCount, array);
    			  }
    			else if (strcmp(argv[1], "-t") == 0)
    			  {
    			    readFile(argv[2], &wordCount, array, 1);
    			    writeToFile(argv[3], wordCount, array);
    			    printTime(begin);
    			  }
    			else
    			  {
    			    usage(argv[0]);
    			  }
    			break;
    		}
    		case 5:
    		default:
    	        usage(argv[0]);
    	        break;
    	}
    	return 0;
    }
    
    // Define all your functions here
    void printTime(time_t  begin)
    {
    	printf("\nThe time at which it began was %ld", begin); 	
    }
    
    void usage(char* processName)
    {
      printf("Usage %s [-s] [-t] inputFileName outputFileName\n", processName);
    }
    			 
    int readFile(char* inputFileName, int* wordCount, char array[][MAX_WORD_LENGTH], int flag)
    {
    	FILE *fp;
    	fp = fopen(inputFileName, "r");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    
    	}
    
    	if(flag == 0)
    	while(fscanf(fp, "%s", array[(*wordCount)++]) > 0);
    	else if(flag == 1)
    	{
    		int i = 0, j= 0, k= 0;
    		//sort the elements of the array
    		while(fscanf(fp, "\n%s", array[(*wordCount)++]) > 0)
    		{
    			if(*wordCount> 1)
    			{
    				for(i =0 ;i<*wordCount; i++)
    				{
    					j = strcmp(array[i],array[*wordCount]);
    					if(j > 0)
    					{
    						k = *wordCount;
    						strcpy(array[k+1], array[k]);
    						for(k = *wordCount; k > i; k--)
    							strcpy(array[k], array[k-1]);
    
    						break;
    					}
    				}
    			}
    		}
    	};
    	fclose(fp);
    
    	return 0;
    }
    
    int writeToFile(char* outFileName, int wordCount, char array[][MAX_WORD_LENGTH])
    {
    	int i = 0;
    	FILE *fp;
    	fp = fopen(outFileName, "w");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    	}
    	
    	for(i = 0; i< wordCount-1; i++)
    	{
    		fprintf(fp, "%s\n", array[i]);
    	}
    
    	fclose(fp);
    	return 0;
    }
    I have a file dictionary.txt which has some 41000 plus words in it and i want to read it. So how do i overcome this problem of stack overflow...?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. make array static or global
    2. or make it dynamically allocated using malloc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    I tried making it global but the problem persists though now the error is not stack overflow but its access violation reading some location.

    I dont want to make it static because if i make it static i would have to initialize the elements at that point and i would not be able to change it.

    char array = malloc(sizeof (char) * (MAX_WORDS * MAX_WORD_LENGTH));

    Is this the right way to do malloc of a 2d array.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Okay i figured out the way of doing dynamic memory allocation for a 2d array using
    [insert]
    Code:
    char **array2d;
    array2d = malloc(sizeof(char *) * MAX_WORDS);
    
                      for(i = 0; i< MAX_WORDS;i++){
    	  array2d[i] = malloc(sizeof(char) * MAX_WORD_LENGTH);
    	  }
    but the thing is that the problem still persists. Any ideas on what should be done?

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    in your loops add checks that you do not access elements out of bonds of your array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Still unable to make any progress. The same problem is still happening. Could it be a IDE or VS issue that the file size is very large and it is not able to allocate space for this large file size.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by roaan View Post
    Still unable to make any progress. The same problem is still happening. Could it be a IDE or VS issue that the file size is very large and it is not able to allocate space for this large file size.
    No, I can almost guarantee you that the compiler is not the problem.

    Wrap your code in message blocks. For example, put things like "Entering inner loop" and "Exiting innerloop" everywhere you are accessing the array. Once you have an enter and no exit, you can drill down until you hit the problem point in the code.

    Part of the problem is going to be right here
    Code:
    					if(j > 0)
    					{
    						k = *wordCount;
    						strcpy(array[k+1], array[k]);
    						for(k = *wordCount; k > i; k--)
    							strcpy(array[k], array[k-1]);
    
    						break;
    					}
    Even though you are shifting everything to the left, you aren't decrementing wordcount. That can lead to issues with grabage being left in the stream, or getting copied to the left. Since C str functions look for the Null character. chances are that it is copying more then it is supposed to (Resulting in an out of bounds error).

    This is one of the reasons you should ALWAYS use a function like strncpy that designate buffer size. You are just asking for trouble by letting c go to the null.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Use the debugger instead.
    For example, using VS, you can add breakpoints. Add a breakpoint that breaks when the memory address changes at each array + sizeof(array). There's a good chance you'll catch some buffer overruns.
    You can access this in Debug -> New -> New Data Breakpoint
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Post the exact error or crash message you are getting including any random looking memory address it gives you. It might not mean much to you, but it often helps others know exactly what the problem is.

    You might also be best to post an updated copy of your code, excluding bits that aren't related to the problem, but enough that it still compiles.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Here is the updated code

    [insert]
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    
    #define MAX_WORD_LENGTH 31
    #define MAX_WORDS 42000
    
    int readFile(char* inputFileName, int* wordCount, char array2d[][MAX_WORD_LENGTH], int flag);
    //int qsort(char array2d[][MAX_WORD_LENGTH], int* wordCount);
    
    int writeToFile(char* outFileName, int wordCount, char array2d[][MAX_WORD_LENGTH]);
    void printTime(time_t  begin);
    void usage(char* processName);
    // enter any other function prototype you may wish
    
    int main(int argc, char* argv[])
    {
    		char array2d[MAX_WORDS][MAX_WORD_LENGTH];
    		int wordCount = 0;
    		time_t begin;
    
    		time(&begin);
    		printf("\n Beginning");
           	switch (argc)
    		{
    		case 3:
    			readFile(argv[1], &wordCount, array2d,0);
    			writeToFile(argv[2], wordCount, array2d);
    			break;
    		case 4:
    		{
    			if (strcmp(argv[1], "-s") == 0)
    			  {
    			   readFile(argv[2], &wordCount, array2d,0);
         		   writeToFile(argv[3], wordCount, array2d);
    			  }
    			else if (strcmp(argv[1], "-t") == 0)
    			  {
    			    readFile(argv[2], &wordCount, array2d, 0);
    			    writeToFile(argv[3], wordCount, array2d);
    				printTime(begin);
    			  }
    			else
    			  {
    			    usage(argv[0]);
    			  }
    			break;
    		}
    		case 5:
    		default:
    	        usage(argv[0]);
    	        break;
    
    		
    	}
    	return 0;
    }
    
    // Define all your functions here
    void printTime(time_t  begin)
    {
    	printf("\nThe time at which it began was %ld", begin); 	
    }
    
    int readFile(char* inputFileName, int* wordCount, char array2d[][MAX_WORD_LENGTH], int flag)
    {
    	FILE *fp;
    	fp = fopen(inputFileName, "r");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    
    	}
    
    	if(flag == 0)
    	while(fscanf(fp, "%s", array2d[(*wordCount)++]) > 0);
    	fclose(fp);
    
    	return 0;
    }
    
    int writeToFile(char* outFileName, int wordCount, char array2d[][MAX_WORD_LENGTH])
    {
    	int i = 0;
    	FILE *fp;
    	fp = fopen(outFileName, "w");
    
    	if(fp == NULL)
    	{
    		printf("\n Unable to open the file. Aborting");
    		exit(EXIT_FAILURE);
    	}
    	
    	for(i = 0; i< wordCount-1; i++)
    	{
    		fprintf(fp, "%s\n", array2d[i]);
    	}
    
    	fclose(fp);
    	return 0;
    }
    
    void usage(char* processName)
    {
      printf("Usage %s [-s] [-t] inputFileName outputFileName\n", processName);
    }
    And the error message is:

    Unhandled exception at 0x00411657 in classc2.exe: 0xC00000FD: Stack overflow.
    Last edited by roaan; 09-08-2009 at 05:48 PM. Reason: File has 42000 words instead of 50 words.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > char array2d[MAX_WORDS][MAX_WORD_LENGTH];
    This is 1.3MB, which is larger than the default 1MB most systems give you for a stack.

    You used vart's good advice and got "but its access violation reading some location. " instead.

    > while(fscanf(fp, "%s", array2d[(*wordCount)++]) > 0);
    So what stops this overflowing the space?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by Salem View Post
    > char array2d[MAX_WORDS][MAX_WORD_LENGTH];
    This is 1.3MB, which is larger than the default 1MB most systems give you for a stack.

    You used vart's good advice and got "but its access violation reading some location. " instead.

    > while(fscanf(fp, "%s", array2d[(*wordCount)++]) > 0);
    So what stops this overflowing the space?
    I didnt get by what you mean so what stops this overflowing the space. Isnt fscanf supposed to return -1 when it reaches the end of file. And i have allocated more space than is required for my static array. So it should continue to read into the array.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Vart means that you cannot allocate more than 1MB of memory. It will try to allocate 1.3MB but will fail and give you a stack overflow error. So you don't actually allocate that memory.

    Check this out though. Put a breakpoint or a printf("a\n") under
    Code:
    char array2d[MAX_WORDS][MAX_WORD_LENGTH];
    and see if it crashes at that point.

    Use your solution to fix this problem even if thare is still a problem. One thing at a time
    Code:
    char **array2d;
    array2d = malloc(sizeof(char *) * MAX_WORDS);
    for(i = 0; i< MAX_WORDS; i++){
    	  array2d[i] = malloc(sizeof(char) * MAX_WORD_LENGTH);
    }

  14. #14
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes i did that of putting a printf and i understood the fact that i cannot allocate more than 1 Mb of memory. It does not print the contents of printf after the array and it crashes before that saying there is a stack overflow.

    I tried using the dynamic memory allocation but there is this error occuring

    Unhandled exception at 0x10296551 in classc2.exe: 0xC0000005: Access violation writing location 0x0081a000.

  15. #15
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, do some debugging. Look where it crashes. Either breakpoints or prinft("OK\n");

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

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