Like Tree4Likes
  • 1 Post By Adak
  • 1 Post By QuantumPete
  • 2 Post By ZuK

Mysterious error

This is a discussion on Mysterious error within the C Programming forums, part of the General Programming Boards category; Okay so I can't post all the code for this but I would like to describe a problem that I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    18

    Mysterious error

    Okay so I can't post all the code for this but I would like to describe a problem that I am having and see if anyone has any ideas what the cause could be. I have a program which has a large loop in which it creates fills in integer strings in structures. I then take the last value from each of these strings and place it into an array because I have to overwrite the old strings but need the last value of the string later. in the end this results in an array of size [32][1000] because it has saved in it the terminal results of 32 different strings from 1000 runs of the loop. The values are set into the array correctly which I have verified. Later, after having never having done anything to the array since I put in the values I print the values from it to a file. The results are almost all correct except for one strange problem. For the first row of the array ([0][0] to [0][999]) I get about 10 correct values, then about 70 values which are all 0 (I checked that when the values were first put into the array the were NOT 0's), then about 20 more correct numbers, then another 70 or so 0's, and then correct numbers until the end of the row. All other rows of the array are completely correct. Can anyone think of a possible cause for this strange pattern?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me check:

    Seer? Nope, on mystical drugs.
    Crystal Ball? Nope, cloudy today.
    Prophet? Nope, not talking to God this morning.

    Guess you'll have to post up at least the code for that loop - something as simple as possible, but shows the problem, would be A-1.
    stahta01 likes this.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by deeisenberg View Post
    Can anyone think of a possible cause for this strange pattern?
    Illegal memory accesses resulting in undefined behaviour?
    stahta01 likes this.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    a bug in your program?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    Okay, so things have gotten even weirder but I have located the spot where the data loss occurs.

    Code:
                matrix[0][19]=matrix[0][19];
                for(ri=0; ri<2000; ri++) ran[ri]=genrand_int32();
                ri=0;
                b[0].trait[0]=1;
                b[1].trait[0]=1;
                for(bi=0; bi<64; bi++) for(i=0; i<74; i++) b[bi].counter[i]=0;
                for(i=1; i<74; i++) { /*Sim along b1*/
                    if(b[0].trait[i-1]) {
                        b[0].trait[i]=gen();
                        if(!b[0].trait[i]) b[0].counter[i+1]++;
                    }
                    else {
                        b[0].trait[i]=rev(b[0].counter[i], ii);
                        if(!b[0].trait[i]) b[0].counter[i+1]=b[0].counter[i]+1;
                    }
                }
                matrix[0][19]=matrix[0][19];
    At the beginning of this section of code matrix[0][19]=1. At the end matrix[0][19]=0.

    This is all within the main loop which does most of the work in my program. The array matrix is local.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,768
    Quote Originally Posted by QuantumPete View Post
    Illegal memory accesses resulting in undefined behaviour?
    My vote is this.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Without all your array and struct declarations as well, there isn't a lot we could say.

    Array overrun seems as good a bet as any.
    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.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    18
    The array isn't overrun because it happens within that section of code in one run. The declarations were...

    Code:
    	int matrix[32][2000]; /*Terminal character states*/
    	struct branch { /*Branches*/
    		char trait[74];
    		int counter[75];
    	}   b[62];

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by deeisenberg View Post
    The array isn't overrun because it happens within that section of code in one run. The declarations were...

    Code:
    	int matrix[32][2000]; /*Terminal character states*/
    	struct branch { /*Branches*/
    		char trait[74];
    		int counter[75];
    	}   b[62];
    look at this
    Code:
    for(bi=0; bi<64; bi++) for(i=0; i<74; i++) b[bi].counter[i]=0;
    there are only 62 b's
    you access 64

    mystery solved

    Kurt
    Last edited by ZuK; 04-30-2012 at 04:54 PM.
    Salem and dmh2000 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mysterious seg fault!
    By MK27 in forum C++ Programming
    Replies: 12
    Last Post: 03-12-2010, 02:29 AM
  2. Mysterious error - missing { before *
    By ulillillia in forum C Programming
    Replies: 4
    Last Post: 03-24-2009, 11:52 AM
  3. Replies: 1
    Last Post: 12-19-2008, 05:32 AM
  4. Mysterious Linker Error
    By Night_Blade in forum Windows Programming
    Replies: 0
    Last Post: 04-30-2006, 02:50 PM
  5. Mysterious message from vVv
    By sean in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 06-08-2002, 05:11 PM

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