Thread: printf statements effecting program

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    printf statements effecting program

    I wrote a function that takes in a string and prints out all permutations of that string. it does this by generating random strings using the characters in the original string, and if this string is unique, it will keep it. otherwise it generates a new random string

    while debugging i had some printfs in there. here is one

    Code:
       char guess[strlen(string)+1];
       char permutations[numOfPermutations][strlen(string)+1];
    ...
             } else {
                printf("%s != %s %d\n", guess, permutations[i], i);
             }
    commenting out that statement breaks the program and I dont understand why. printfs are not meant to have side effects in a program. yet somehow this one does.

    this has happened to me before and I'd like to know the cause of it. it may be a beginner problem, i havent been programming for that long

    I can post more/all of the code if needed

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Can u paste the whole function dude

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    sure

    anything in uppercase would be a #define'd constant

    Code:
    int main (void) {
    
       srand(time(0));
    
       testUnique();
    
       permutation ("abc");
    
       return EXIT_SUCCESS;
    
    }
    
    int allUnique(char *string) {
    
       int length = strlen(string);
       int i,j;
       for (i=0; i<length;i++) {
          for (j=i+1; j<length;j++) {
             if (string[i] == string[j]) {
                return 1;
             }
          }
       }
       return 0;
    
    }
    
    
    void permutation (char *string) {
    
       int length = strlen(string);
       int prime = 1;
       int numOfPermutations = 1;
       int i, j=0;
       int random;
       char guess[length+1];
       int same;
    
       for (i=length; i>0; i--) {
          numOfPermutations *= i;
       }
    
       char permutations[numOfPermutations][length+1];
       int upTo = 0;
    
       strcpy(permutations[upTo], string);
       upTo++;
    
       int index;
    
       while (upTo != numOfPermutations) {
          index = 0;
    
          while (index<length) {
             random = rand() % length;
             guess[index] = string[random];
             index++;
          }
          same = FALSE;
          for (i=0; i<upTo && same == FALSE; i++) {
    
             if (strcmp(guess, permutations[i]) == 0 ) {
                same = TRUE;
                i = upTo;
    
             } else {
                printf("first  %s != %s %d\n", guess, permutations[i], i);
              /*************
               problem here
               ************/
             }
    
          }
    
    
          if (same == FALSE && allUnique(guess) == 0) {
             // this indicates that the break condition for the previous for loop
             // was i<upTo, that is, we found a unique string
             printf("string %s is unique\n", guess);
             strcpy (permutations[upTo], guess);
             upTo++;
    
          } else if (strcmp (guess, "abc") == 0 && prime%ABC!=0) {
    
             printf("second %s\n", guess);
    
              /*************
               problem here
                ************/
    
          }
    
    
    
       }
    
       printf("showing all permutations\n");
    
       for (i=0; i<numOfPermutations; i++) {
          printf("%s\n", permutations[i]);
       }
    
    }

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Brain_Child View Post
    commenting out that statement breaks the program and I dont understand why. printfs are not meant to have side effects in a program. yet somehow this one does.
    It means your program is buggy. printf() has no side effects. However, calling a function alters the contents of the stack. If you have corrupted the stack, or are accessing it incorrectly, then the alterations printf() makes to the stack can make your program seem to work (or not work).

    Somewhere, you have a local variable which is array. You've corrupted this array.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. menu problem!!! need U R G E N T help!!!
    By catcat28 in forum C Programming
    Replies: 16
    Last Post: 11-19-2007, 01:32 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  4. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  5. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM