Thread: Vector Element spontaneously changing?!

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Question Vector Element spontaneously changing?!

    Hello,

    I am somewhat of a newbie when it comes to programming in C, although I am familiar with Fortran. I have been experiencing unrequested changes to certain elements in a vector while using a for-loop. My code for this section is as follows:

    Code:
     for (i=1;i<NZ;i++) {
       a[i]=-eta2;    // defining the entries in the tridag matrix
       b[i]=eta1;        // defining the entries in the tridag matrix
       c[i]=-eta2;     // defining the entries in the tridag matrix
       printf("%f should be %f and not %f \n",b[1],eta1,eta2);
       }
    where NZ, eta1 and eta2 are read in from a parameter file upon running the compiled program. The program compiles without any errors, but once executed I am presented with the following output:

    5.201031 should be 5.201031 and not -2.100515
    5.201031 should be 5.201031 and not -2.100515
    5.201031 should be 5.201031 and not -2.100515
    5.201031 should be 5.201031 and not -2.100515
    -2.100515 should be 5.201031 and not -2.100515
    with the final line repeating until the counter reaches NZ (in this case, 100). As can be seen from the above output, b[1] seems to change without reason when i=4 from eta1 to -eta2. It's also worth noting that a[1] and c[1] are unchanged throughout this loop.

    Does anyone have any ideas as to what might be causing this strange behaviour? A compiler bug (surely not)? I am using gcc 3.3 on Mac OS X 10.3.3.

    Any help will be greatly appreciated! Many thanks,


    -Matt

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, for array indexes, your array starts at 0, and not 1. Thus, if your array holds 4 itesm, you have valid array indexes as:
    Code:
    int array[4];
    
    /* valid */
    array[0] = 1;
    array[1] = 2;
    array[2] = 3;
    array[3] = 4;
    
    /* Not valid: */
    array[4] = 5; /* error, you're accessing beyond your array */
    Is that your problem?

    Otherwise, post your array declarations as well as what "eta1" and "eta2" are.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    make sure you read eta1 and eta2 as integers or digits not as characters. and arrays start as 0 in C not as 1make sure you aren't overwriting you bounds[edit]Beaten again[/edit]

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    3
    OK, my declarations relating to the snippet of code are:

    Code:
    float *a; // vectors used in tridag
    float *b;
    float *c;
    float eta1,eta2;
    with the array sizes of a, b and c defined as:

    Code:
    	a=(float *)malloc((NZ+1) * sizeof(float));     // setting the vector size
    	b=(float *)malloc((NZ+1) * sizeof(float));
    	c=(float *)malloc((NZ+1) * sizeof(float));
    Also, while this doesn't directly relate to my problem, b[0] and b[NZ] are allocated values immediately after the for-loop, namely

    Code:
     // defining the boundary conditions in the tridag matrix
     a[0]=0.0;
     b[0]=eta3;
     c[0]=-eta4;
    
     // fix the boundary at NZ
     a[NZ]=0.0;
     b[NZ]=1.0;
     c[NZ]=0.0;
    Thanks for the responses!


    -Matt

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you hard code the values for NZ and eta1 to something, in just a small program, does it work?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main( void )
    {
        float *a, *b, *c;
        float eta1 = 5.201031, eta2 = -2.100515;
        int x - 0; NZ = 6;
    
        a = malloc( sizeof(*a) * NZ );
        if( a == NULL ) exit(0);
        b = malloc( sizeof(*b) * NZ );
        if( b == NULL ) exit(0);
        c = malloc( sizeof(*c) * NZ );
        if( c == NULL ) exit(0);
    
        for( x = 0; x < NZ; x++ )
        {
            a[x] = -eta2;
            printf("a[%d] is %f and should be %f\n", x, a[x], -eta2 );
            b[x] = eta1;
            printf("b[%d] is %f and should be %f\n", x, b[x], eta1 );
            c[x] = -eta2;
            printf("c[%d] is %f and should be %f\n", x, c[x], -eta2 );
    
        }
        free( a );
        free( b );
        free( c );
    
        return 0;
    }
    I think that's right... anyway, do something like that to test it by itself with hard coded values.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    3
    The mini-program works properly (using the line ` int x, NZ = 6; ` ). However, even with hard-coded values in my larger program the same problems are occurring. For example, using the lines:

    Code:
        for( i = 1; i < NZ; i++ )
        {
            a[ i ] = -eta2;
            printf("a[%d] is %f and should be %f\n", i, a[ i ], -eta2 );
            b[ i ] = eta1;
            printf("b[1] is %f and should be %f\n", b[1], eta1 );
            printf("b[%d] is %f and should be %f\n", i, b[ i ], eta1 );
            c[ i ] = -eta2;
            printf("c[%d] is %f and should be %f\n", i, c[ i ], -eta2 );
        }
    gives

    a[1] is -2.100515 and should be -2.100515
    b[1] is 5.201031 and should be 5.201031
    b[1] is 5.201031 and should be 5.201031
    c[1] is -2.100515 and should be -2.100515
    <SNIP>
    a[4] is -2.100515 and should be -2.100515
    b[1] is 5.201031 and should be 5.201031
    b[4] is 5.201031 and should be 5.201031
    c[4] is -2.100515 and should be -2.100515
    a[5] is -2.100515 and should be -2.100515
    b[1] is -2.100515 and should be 5.201031
    b[5] is 5.201031 and should be 5.201031
    c[5] is -2.100515 and should be -2.100515
    EDIT: I forgot to mention that the last four lines of output repeat, changing with i and b[1] giving the same (incorrect) value, until i reaches NZ-1 (=99)

    The very fact that this can happen in a self-contained loop is quite disconcerting. Has anyone ever experienced anything similar to this? Anyway, I will re-write various parts of my code tomorrow and report back in the evening. Thanks to all for their help.


    -Matt
    Last edited by homgran; 05-29-2004 at 05:37 AM. Reason: Clarification of output

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Check the value of NZ with printf() right before you allocate the memory. My bet is that it is different to what you think it is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing a tree into a list question..
    By transgalactic2 in forum C Programming
    Replies: 20
    Last Post: 11-11-2008, 08:40 AM
  2. Changing a variable to a constant.
    By esben in forum C Programming
    Replies: 6
    Last Post: 03-22-2006, 06:29 AM
  3. Changing windows without changing?
    By Lionmane in forum Windows Programming
    Replies: 7
    Last Post: 10-19-2005, 11:41 AM
  4. [C++/WinAPI] Changing bitmap contrast
    By jagi in forum Windows Programming
    Replies: 0
    Last Post: 03-27-2005, 03:51 PM
  5. changing the desktop background c++
    By v0id in forum Windows Programming
    Replies: 1
    Last Post: 02-21-2002, 08:03 PM