strlen causing my program to freeze

This is a discussion on strlen causing my program to freeze within the C Programming forums, part of the General Programming Boards category; I am having an interesting problem involving calling strlen - it's causing my program to stop responding. I've isolated it ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Question strlen causing my program to freeze

    I am having an interesting problem involving calling strlen - it's causing my program to stop responding. I've isolated it down to just that one line. Funny thing is that I use it a little bit before in the code seemingly the same and I have no problems. The line in bold is the problem line:

    Code:
    [...]
    } else if(aa[ii][0] == '>') {
    
                // push
                if(ii == stackSize) {
                    fprintf(stderr, "Full stack.\n");
                } else {
                    printf(strlen(aa[ii]));
                    ii++;
                }
    
    }
    [...]
    aa[ii] is a string of user input. I know that aa[ii] has a legitimate value. Here is a bigger chunk of code if you need it:

    Code:
        int cc;
        ii = 0;
        for(cc = 0; cc < 1; cc--) { // infinite loop
            if(fgets(aa[ii], LINE_SZ, stdin) == NULL)
                exit(0);
    
            if(strlen(aa[ii]) > 80) {
    
                fprintf(stderr, "Bad input.\n");    
        
            } else if(aa[ii][0] == '<') {
    
                // pop
                if(ii == 0) {
                    fprintf(stderr, "Empty stack.\n");
                } else {
                    printf(aa[ii-1]);
                }
    
            } else if(aa[ii][0] == '>') {
    
                // push
                if(ii == stackSize) {
                    fprintf(stderr, "Full stack.\n");
                } else {
                    printf(strlen(aa[ii]));
                    ii++;
                }
    
            } else {
    
                // bad input
                printf("Bad input.\n");
    
            } 
    
                    
        }
    Before that chunk I call
    Code:
    arg_length = strlen(argv[1]);
    and it works fine. I don't understand what is causing my program to freeze (I run it through the command prompt on windows xp, and when it reaches that line it stops responding and gives me that 'this program stopped responding and needs to be shut down' message). And I know it is exactly that line because I isolated it doing error tests.

    Any idea? Thanks!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Problem has nothing at all to do with strlen(). You are not calling printf() the right way.

    Code:
    printf("%d", strlen(aa[ii]));

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf(strlen(aa[ii]));
    How about enabling warnings in the compiler, so that when you mess up the call to printf by forgetting the format string, you actually get told about it?

    In case you've forgotten, the prototype for printf is:
    Code:
    int printf(const char *fmt, ...);
    strlen is not a char *, so the compiler should be able to tell that it's wrong.

    --
    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. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM

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