Thread: my program murdered printf!

  1. #1
    Registered User
    Join Date
    May 2007

    my program murdered printf!

    i'm using a windows XP.

    Here's my code, but most of it is extraneous to my question. I am concerned with the section highlighted in red.
    Edit : i realize that red is an eyesore, but so are all the other colors!!!!!!;

    #define true (char)1;
    #define false '\0'
    #define GEN struct generation
    typedef unsigned char boolean;
    struct {int* list; int length;} numList;
    struct coordinates {int x; int y;} pos;
    struct generation {boolean val; struct generation *next;};
    int data[128];
    int special=0,superspec=0;
    void check(int);
    void freeList(GEN*);
    #include <random.h>
    #include <random.c>
    #include <stdio.h>
    //int CRAP = 43;
    //#undef NULL
    //#define NULL &CRAP
    void main()
       static unsigned int arr[128];
       int i;
       for (i=0; i < 128; i++)
        arr[i] = i;
       numList.list = arr; //printf("%d\n\n\n\n",sizeof(int)); 1
       numList.length = 128;
       for (i=0; i < 128; i++)
          int j;
          data[i] = getRand(0, numList.length-1);
          for (j=data[i]; j < numList.length; j++)
           numList.list[j] = numList.list[j+1];
    void freeList(GEN *gen)
       if (gen->next != NULL && gen->next != gen)
       special |= ((int)gen->val?1:0)<<superspec++;
    void check(int in)
       GEN *root;
       int out = in + 1;
       int x, cur = 0, i;
       unsigned int y;
       boolean prev = false;
       GEN *last, *end;
       root->next = root;
       root->val = false;
       last->val = end->val = root->val;
       last->next = end->next = root->next;
       for (i = 0; i < sizeof(int)*8 && in/(1<<i); i++)
          GEN *nNode;
          //printf("1dog\n"); 2
          //printf("2dog\n"); 3
          if (nNode == NULL)
          nNode->next = last; //printf("3dog\n"); 4
          nNode->val = (1<<i) & in; //printf("4dog\n"); 5
          last = nNode; //printf("5dog\n"); 6
       x=i; //printf("%d\n",x); 7
       end->next = last;
        //freeList(end); 8
        printf("%d\n",special); 9
       while (cur != out)
          int t=x;
          prev = false;
          last->val = end->val = true;
          last->next = end->next = end;
          while (x-- > -sizeof(int))
             boolean temp=prev, next=last->next->next->val;
             cur = (cur<<1) | ( (int)last->next->val?1:0 );
             prev = last->next->val;
             last = last->next;
    	 if (last!=root)
                if (  temp && prev && next ||
                    !(temp || prev || next) ||
                     temp && !(prev || next) )
                 last->val = false;
                 last->val = true;
             if (cur == out)
              goto finish;
          x = t+1;
       while (last != root)
          boolean temp=prev, next=last->next->next->val;
          prev = last->next->val;
          last = last->next;
          if (  temp && prev && next ||
              !(temp || prev || next) ||
               temp && !(prev || next) )
           last->val = false;
           last->val = true;
       pos.x = x; pos.y = y;
       printf("%d+1==%d at (%d, %d)\n", in, out, x, y);
    As you can see, I have various commented out code segments or debugging printf's. these are labeled in blue for future reference.

    All red printf's are meant for debugging purposes.

    depending on the value of "special" and which of the different blue-labeled code segments i have enabled (as in not commented out) i get radically different output. the number of newlines in segment 1 can determine what value is printed in segment 9 or whether my program terminates prematurely, and sometimes segment9 has printed stuff to the screen and then the program blocked, without accepting input (that is, if i typed a letter, it would not appear on the screen, and typing a letter and pressing enter would not terminate the program).

    the "dog" printf's (segments 2 thru 6) always worked fine, as did segment 1. calling segment 8 can yield weirder or more normal output, depending on the situation. there have been times when my cursor was placed in already-printed text, so that by typing a letter i would overwrite stuff that had already been printed out by the program.

    depending on different combinations of enabled segments, i have gotten outputs that varied somewhat from execution to execution (tho still followed some patterns) and outputs that were senseless but were the same every execution. printing out stuff before the weirdness happens seems to (in general) make the weird stuff less weird (and more concise).

    the value of special, when it is distinguishably printed out, is either 1, 2, -255, or -254, depending on the value i initialize it to and on the number of printf's in segment 1.

    now, finally for some sample output:

    M ╙}

    ♠2♦■±↕☺ ∞ ¶╨♥|♥ ¶Σ☺▬■¶◄1 Γ☺╨♥|♥ ╩..H 1 Γ☺ ☻ ☺% ╢.. ↕¡↕tEΦ
    ....☺ ....☺ ☺ {♥
    !5|► òC┤d╨.. ' ♦ ─..IB¡1 ╬dw3← '
    ∞B╨..╘..░☺Γ☺▄..ε..ù☺☺ α..á↨ ∞☺..♠ ♠ 6 ∞☺°..

    the above is one of the most common (and one of my favorites!). it is produced by the code above. it retains this format and many of the early and middle characters, but the latter characters differ somewhat for each execution.

    I just noticed that when i executed this in command prompt (rather than double clicking it in my computer) the directory is subsequently altered.

    'dd' is not recognized as an internal or external command,
    operable program or batch file.


    (it should be c:\documents and settings\nick\my documents\c)

    more output...


    (there is an empty line before the one in the output)

    the above out put is just like the code before except that segment 7 has been enabled

    in other words, i think my program is altering its own instructions at run time due to inadvertantly overwriting memory in the for loop, but i am too stupid to figure out where my error is :-(

    Last edited by no1uno; 05-12-2007 at 07:09 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > #define GEN struct generation
    If you're going to do this, use a typedef.

    > void main()
    main returns an int (see the FAQ)

    > numList.list = arr;
    Do you get a warning about types here?

    > nNode=(GEN*)malloc(sizeof(*nNode));
    Don't cast the return result of malloc in C (see the FAQ)

    > if (gen->next != NULL && gen->next != gen)
    What sort of condition is that for a linked list?
    next == NULL
    is all you should need for ending a list.

    I've no idea what you were thinking with your commented out attempt to redefine NULL, but you should forget all about it. That idea is going nowhere.

    > root->next = root;
    > root->val = false;
    > last->val = end->val = root->val;
    > last->next = end->next = root->next;
    None of these variables are initialised (root = something).
    So you just spray assignments all over memory.
    This is where your code is severely broken.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 09:48 PM
  2. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 03:58 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 11:14 AM
Website Security Test