Thread: very weird error with a variable

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    very weird error with a variable

    heres a snippet of some of my code, but im getting a problem with the arrays that i've created..

    whats going on is that i have a 2d array, and a 1d array
    and say my 1d array has
    2.
    2.
    3.
    2.
    2.
    3.
    2.
    5.
    0.
    5.

    what it should do is get each number and square it and put it in the 2d array. but it is not doing that. it does something weird like..

    4
    4
    9
    4
    4
    9
    4 //works up to here and then the next 3 just stuff up
    16
    16
    81



    Code:
       int N = 5;
      int x, i=0;
        int number, row, column, new_col;
    
           array = (int **)malloc(N*sizeof(int));
           new_array = (int **)malloc(N*sizeof(int));
            bleh = (int*)malloc(N*sizeof(int));
     for(x=0; x<N; x++){   
                    array[x]=(int*)malloc(N*sizeof(int));
                    new_array[x]=(int*)malloc(N*sizeof(int));
            }
    
         for(row = 0; row<5;row++)
            {
    
                    if(column>1)
                            new_col++;
    
                    column = new_col;
    
                    for(;column<=5;column++) //second for loop
                    {
                            number = bleh[i];
                            array[row][column] = number * number;
                          new_array[row][column] = number * number;
                            i++;
    
                            printf("array is: %d\n", array[row][column]);
                            printf("new_array is: %d\n", new_array[row][column]);
                    }
    
            }


    any ideas?
    im thinking that array and bleh are somehow linked mysteriously..

    and the thing is when i insert a new variable like new_array it works.. its very strange..

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
            bleh = (int*)malloc(N*sizeof(int));
     for(x=0; x<N; x++){   
                    array[x]=(int*)malloc(N*sizeof(int));
                    new_array[x]=(int*)malloc(N*sizeof(int));
            }
    
         for(row = 0; row<5;row++)
            {
    
                    if(column>1)
                            new_col++;
    
                    column = new_col;
    
                    for(;column<=5;column++) //second for loop
                    {
                            number = bleh[i];
    So where in there do you actually set any of the bleh elements to anything? It looks to me like number is just going to be some garbage value. Also, array and new_array should be allocated N*sizeof(int *) bytes. Betting on the size of an int being the same as the size of a pointer is risky I think. And don't cast the return value of malloc(). That's a C++-ism and isn't recommended in C.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    yep bleh is set up somehwere else, the weird thing that is happening is that when i complete this bit of code
    Code:
    for(;column<=5;column++) //second for loop
                    {
                            number = bleh[i];
                            array[row][column] = number * number;
                          new_array[row][column] = number * number;
                            i++;
    
                            printf("array is: %d\n", array[row][column]);
                            printf("new_array is: %d\n", new_array[row][column]);
                    }
    it changes the variables in the 1d bleh variable, when here i dont have anything that has bleh = ...

    if i put in printf statements like so..
    Code:
    printf("gg1= %d %d %d %d\n", gg[6],gg[7],gg[8],gg[9]);
    
    //above for loop goes here
    
    
    printf("gg2 = %d %d %d %d\n", gg[6],gg[7],gg[8],gg[9]);
    print out is

    gg1 = 2 5 0 5
    array is: 4
    array is: 4
    array is: 9
    array is: 4
    gg2 = 2 4 4 9
    gg1 = 2 4 4 9

    and so you can see that gg has changed??! it should stay as 2 5 0 5

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What do you mean set up somewhere else? There's a chunk of code missing in the middle of what you originally posted? It would be a lot simpler if you showed us all of the relevent code (including declaring and setting up the arrays.)
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    really messy code..sorry

    Code:
    int main(int argc, char *argv[])
    {
       FILE *input;
       int N, K, x, pos=4, b, s, r=0, q=0;
       int unequal=0, f=6, g, y=0, m, t;
       int **array=NULL, *bleh, **new_array=NULL;
       int *array;
       char *axe, *wax;
       int J=0, max = 0;
       int max_rows = 0, max_cols = 0, column=0, row=0, new_col;
    
    
      input = fopen(argv[1], "r+");
      fscanf(input, "%d %d", &N, &K);       
            axe = (char*)malloc(K*sizeof(char));    
           wax = (char*)malloc(K*sizeof(char));   
            new_array = malloc(N*sizeof(int*));
            bleh = malloc(N*sizeof(int));
     for(x=0; x<N; x++){   
                 array[x]=(int*)malloc(N*sizeof(int));
                    new_array[x]=malloc(N*sizeof(int));
            }
    
    do
            {
                    fseek(input, pos, SEEK_SET);   
                    fgets(wax, K+1, input);           
                    for(x=0; x<N-1; x++)
                    {
                            fseek(input, pos+1+f, SEEK_SET);  
                            fgets(axe, K+1, input);
                            f+=K+1;
                            unequal=0;
                            for(g=0; g<K; g++)
                            {   
                                    if(wax[g]!=axe[g])
                                            unequal++;      
                            }
                            bleh[q] = unequal;
                            q++;
                    }
                    pos+=K+1;    
                    f=6; N--; y++;
            } while(y<=N+1)
    
            q=0;   t=1;
            max_rows = b; column = r; max_cols = s;
            row = m;   new_col = t;
    
    //and then the for loops i already pasted go here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, weird problem setting variable
    By sloppy_coder in forum C Programming
    Replies: 3
    Last Post: 09-25-2005, 02:58 PM
  2. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  3. variable can auto initialize itself?
    By thinhare in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 06:07 AM
  4. About classes and HINSTANCE variable to Main
    By conright in forum Windows Programming
    Replies: 2
    Last Post: 01-01-2003, 08:00 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM