corrupted stack

This is a discussion on corrupted stack within the C Programming forums, part of the General Programming Boards category; hi. i wrote this program: Code: /* * A program to find prime number pairs, triplets etc. * Prime number ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    corrupted stack

    hi. i wrote this program:

    Code:
    /*
    * A program to find prime number pairs, triplets etc.
    * Prime number pairs are two prime numbers which are an exact number apart.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int checkprime(int, int, int, int);
    
    struct asettings {
    	int high, low, difference, number;
    };
    
    int main(int argc, char *argv[])
    {
    	/* Command line arguments */
    	struct asettings my_settings;
    	my_settings.high = 100;
    	my_settings.low = 1;
    	my_settings.difference = 2;
    	my_settings.number = 2;
    	int i;
    	
    	if (argc != 1) {
    		for (i = 1; i <argc; i + 2) {
    			if (argv[i][0] == '-') {
    				switch (argv[i][1]) {
    					case 'h':
    						my_settings.high = atoi(argv[i+1]);
    						i = i + 1;
    						break;
    					case 'l':
    						my_settings.low = atoi(argv[i+1]);
    						i = i + 1;
    						break;
    					case 'd':
    						my_settings.difference = atoi(argv[i+1]);
    						i = i + 1;
    						break;
    					case 'n':
    						my_settings.number = atoi(argv[i+1]);
    						i = i + 1;
    						break;
    					default:
    						printf("Usage: %s -h highest_number -l lowest_number -d difference -n number_per_group\n", argv[0]);
    						exit(EXIT_FAILURE);
    				}
    			}
    			else {
    				printf("Usage: %s -h highest_number -l lowest_number -d difference -n number_per_group\n", argv[0]);
    				exit(EXIT_FAILURE);
    			}
    			break;
    		}
    	}
    	
    	printf("This is a program to find groups of prime numbers separated by a fixed value.\n\n");
    	printf("Your current settings are: \n");
    	printf("Range for prime number search: %d and %d\n", my_settings.low, my_settings.high);
    	printf("Difference between prime numbers: %d\n", my_settings.difference);
    	printf("Number of prime numbers per group: %d\n", my_settings.number);
    	printf("Program will begin");
    	getchar();
    	/* Error occurs here */
    	
    	int number = my_settings.low;
    	int yes = 1;
    	int counter = 0;
    	int temp, j;
    	while (number < my_settings.high) {
    		yes = checkprime(number, my_settings.difference, my_settings.number, 1);
    		if (yes == 0) {
    			temp = number;
    			counter = counter + 1;
    			printf("%d", number);
    			for (j = 1; j <= my_settings.number; j ++) {
    				temp = temp + my_settings.difference;
    				printf(", %d", temp);
    			}
    			printf(" are a prime number group\n");
    		}
    		number = number + 2;
    	}
    	
    	printf("Total %d groups between %d and %d\n\nEnd of program\n", counter, my_settings.low, my_settings.high);
    	getchar();
    	
    	exit(EXIT_SUCCESS);
    }
    
    int checkprime(int numb, int diff, int total, int current)
    {
    	int divisor;
    	int check;
    	divisor = numb / 2;
    	
    	for (;;) {
    		if (numb % divisor == 0) {
    			break;
    		}
    		else {
    			divisor = divisor - 1;
    		}
    		if (divisor == 1) {
    			if (numb % divisor == 0) {
    				break;
    			}
    			else {
    				if (current < total) {
    					check = checkprime(numb + diff, diff, total, current + 1);
    					if (check == 0) {
    						return (0);
    					}
    				}
    				else if (current == total) {
    					return (0);
    				}
    				break;
    			}
    		}
    	}
    	
    	return(1);
    }
    it compiles fine, but when its running i get a corrupted stack/core dumped error. it occurs at the comment where i stated.

    why doesnt it work? thx for the help!

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Shouldn't this:
    Code:
    for (i = 1; i <argc; i + 2) {
    really say:
    Code:
    for (i = 1; i <argc; i += 2) {
    Secondly, you either need to do the above add, or do the i = i + 1 at each case, (and change it to i += 2), but don't do both. I reckon you are indexing past the end of the argv array.

    I would also validate the proper number of args before I assumed argv[i+1] existed.

    Todd

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > for (i = 1; i <argc; i + 2)
    Does i get incremented here?

    Also, what is the format of the command line you type in?

    If you miss a parameter, then you'll end up doing atoi(NULL) at some point.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Since each of the 4 cases have exactly the same code, you could use switch's fallthrough instead to only write the code once:
    Code:
      case 'h':
      case 'l':
      case 'd':
      case 'n':
        my_settings.high = atoi(argv[i+1]);
        ++i;
        break;

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by robatino View Post
    Since each of the 4 cases have exactly the same code, you could use switch's fallthrough instead to only write the code once:
    Code:
      case 'h':
      case 'l':
      case 'd':
      case 'n':
        my_settings.high = atoi(argv[i+1]);
        ++i;
        break;
    Look again. The four cases are not exactly the same. They are all unique. With your version, only settings.high would ever get set.

    Todd

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    while (number < my_settings.high) {
    Do you perhaps want <=?

    Code:
    printf("Usage: &#37;s -h highest_number -l lowest_number -d "\
        "difference -n number_per_group\n", argv[0]);
    To indicate that those arguments are optional, you might want to use square brackets (which is the accepted syntax as far as I know):
    Code:
    "Usage: %s [-h highest_number] [-l lowest_number] [-d difference]\
    [-n number_per_group"
    While calling exit() from main() is perfectly acceptable, there's absolutely no need (assuming you don't use atexit(), which is the case here). You can just use return statements.

    This is the only place that you use the variable yes:
    Code:
    		yes = checkprime(number, my_settings.difference, my_settings.number, 1);
    		if (yes == 0) {
    You could change that to simply
    Code:
    		if(checkprime(number, my_settings.difference, my_settings.number, 1) == 0) {
    [edit]
    Look again. The four cases are not exactly the same. They are all unique.
    But they're similar enough that there's a lot of duplicate code. Perhaps you could put that code into a function. Or at least simplify the code to one line:
    Code:
    my_settings.whatever = atoi(argv[++i]);
    Or, if you wanted to get really fancy . . .
    Code:
    char lookup[] = "hldn";
    int *pointer[] = {&my_settings.high, &my_settings.low, /* ... */ };
    char *p;
    
    if((p = strchr(lookup, argv[i][1]))) {
        *(pointer[p - lookup]) = atoi(argv[++i]);
    }
    [/edit]
    Last edited by dwks; 11-21-2007 at 02:09 PM.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Todd Burch View Post
    Look again. The four cases are not exactly the same. They are all unique. With your version, only settings.high would ever get set.

    Todd
    You're right, sorry about that.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    i commented out the entire command line argument part but still get the same error:
    Code:
    Error while dumping state (probably corrupted stack)
    Segmentation fault (core dumped)

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How many calls deep do you recurse in checkprime() - add a global/static variable that counts the number of levels and prints every 100 or 1000 levels deep [count up when you enter the function, down when you leave].

    These are the most common ways to get a segmentation fault:
    - Pointer that points to invalid address [uninitialized pointer, NULL].
    - Out of range array/pointer access.
    - Running out of stack, e.g due to too much recursion.

    On the other hand, I don't think this code is right:
    Code:
    		if (divisor == 1) {
    			if (numb % divisor == 0) {
    				break;
    			}
    Isn't X % 1 always 0?

    --
    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.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Looks like out of control recursion to me.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 01:56 AM
  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