Thread: OS X vs Ubuntu compilation and running differences

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    21

    Unhappy OS X vs Ubuntu compilation and running differences

    Hello all,
    I need some help with static arrays. I have a pretty significantly large number of different n by n sized arrays in a program that I have worked on. The arrays are supposed to be symmetric so array[i][j]==array[j][i]. What is happening is that when I compile and run the program on a linux machine running ubuntu its runs just fine but when I compile and run the program on osx it breaks the degeneracy of the matrix. I am running gcc versions:
    gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 vs osx gcc-4.2

    I am guessing the osx is not assigning enough memory space to the program but I have no idea how to go around fixing it. Any ideas, clues or useful hints will be appreciated.
    -H

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Redefine static arrays to dynamic (remember to allocate memory and free when no longer used) .

    Read more about stack and heap.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    21
    thanks for all your help!
    I have that planned but it will take me some time to implement across the entire thing. Any ideas in the mean time to increase the amount of free memory available to the running program?


    Quote Originally Posted by DRK View Post
    Redefine static arrays to dynamic (remember to allocate memory and free when no longer used) .

    Read more about stack and heap.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Arrays declared before main(), are global, and also are created on the heap. Might goof up your logic in your program, I can't say, but there it is.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I am guessing the osx is not assigning enough memory space to the program but I have no idea how to go around fixing it. Any ideas, clues or useful hints will be appreciated.
    So do you just get the "wrong" answer, or does it crash out with a segmentation fault?
    If it were purely down to lack of memory, it would just stop.

    If on the other hand you're getting different results from your program, then it seems far more likely that it is a bug in your program.
    You have no idea how many times we see code posted here which contains bugs, but just happens to get "lucky" and produces the expected output.

    How much effort would it be to produce a stripped down program which replicates the problem you're seeing (small enough to be posted here anyway).
    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.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    21
    thanks for all your help!
    I get the "right" answer on the linux machine since I have worked out the dataset before as a control. My group people told me that the program was giving very strange results in osx and that is what got me started down this hellish rabbit hole. What I get in osx is really strange outputs because the degeneracy of various matrices has been broken.


    It would actually be quite difficult to get a stripped down version of the program. But I will try to explain it:
    I basically have a bunch of different global arrays of varied sizes. I read data off an input file into the array. When I try to initialize the other global arrays, somehow the program destroys the degeneracy of the first rows columns of my data arrays(so arrray[0][250]!=array[250][0] etc).

    My guess is that initializing the other arrays is somehow "overwriting" the first few portions of this array. I tried running the same exact code in a linux machine with the same checks in place and it doesn't break the data array. Its almost definitely an osx problem.

    Quote Originally Posted by Salem View Post
    > I am guessing the osx is not assigning enough memory space to the program but I have no idea how to go around fixing it. Any ideas, clues or useful hints will be appreciated.
    So do you just get the "wrong" answer, or does it crash out with a segmentation fault?
    If it were purely down to lack of memory, it would just stop.

    If on the other hand you're getting different results from your program, then it seems far more likely that it is a bug in your program.
    You have no idea how many times we see code posted here which contains bugs, but just happens to get "lucky" and produces the expected output.

    How much effort would it be to produce a stripped down program which replicates the problem you're seeing (small enough to be posted here anyway).

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well array overrun looks a very strong candidate to me, given your latest description.

    > I read data off an input file into the array.
    You don't have any while (!feof(fp)) loops do you?

    Because this is almost certainly wrong, and frequently leads to an extra element being 'read(*)' from the file. If you sized your arrays to exactly match the file, then you would end up with an array overrun.


    (*) Since it was almost at end of file, what is typically seen is the last data in the file being duplicated.
    Code:
    $ gcc foo.c
    $ ./a.out 
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp = fopen("foo.c","r");
        char buff[BUFSIZ];
        while ( !feof(fp) ) {
            fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
        }
        rewind(fp);
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            fputs(buff,stdout);
        }
        fclose(fp);
        return 0;
    }
    /* the last line */
    /* the last line */
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp = fopen("foo.c","r");
        char buff[BUFSIZ];
        while ( !feof(fp) ) {
            fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
        }
        rewind(fp);
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            fputs(buff,stdout);
        }
        fclose(fp);
        return 0;
    }
    /* the last line */
    As you can see, the broken feof() loop causes the last line of the file to be output twice. The very last fgets() call read no data at all, but left the previous buffer unmodified. Unless you check the fgets() itself for success, you could well end up processing the same buffer again.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    21
    yea reading an exclusion criteria for my input files seems to be whats causing the problem. No idea why the thing only breaks in macs though.


    the input for my exclusion criteria is a single file called exclusion_list.txt which has a few hundred lines and bunch of space separated numbers in each
    my code is

    Code:
    printf("Reading the cutoff (exclusion_list.txt) file\n");
    
    FILE *four = fopen("exclusion_list.txt", "r");
      i=0;//setting i=0 to keep track of how many residues we have gone through
        if(four==NULL)
    	 {
    	  printf("ERROR:The exclusion_list.txt file does not exist.\n");
    	  return 0;
    	}
        else
    	{
    	  while( i < number_of_res)//only the first number_of_res lines will be read, others will be disregared
    	  {
    	  while(fgets(line, LINE_MAX, four))//gets a new line for each index i
    	  {
    	    pch =strtok(line,"\n");//since the newline character is included in fgets we need to get rid of it. the pch has the entire line with spaces except for the new line character
    	    pch = strtok (pch," ");//get the first number from the string list stored in the pch
    	    while (pch != NULL)//if the number is not empty(end of line)
    		{
    
    //actual processing of these numbers. 
    
    }
    pch = strtok (NULL," ");//get the next number and loop continues.
    		}
    	  }
    	      i++;//get the next index
    	  }
    	}
    	
    fclose(four);
    }//end of reading
    what i hope the code should do is read the following line

    34 43 52 54 64
    get rid of the new line character
    34 43 53 54 64
    and then feed individual numbers starting with the first to main program.

    I know i should have been using a fscanf or something along those but the code was working on linux so I didn't bother.


    Thanks for your help. I am going to either modify the code to make it more robust or modify the input files. Either way the error should hopefully go away. Thanks again.
    ps wow at your ability to diagnose what I was doing incorrectly by just reading some vague description.


    Quote Originally Posted by Salem View Post
    Well array overrun looks a very strong candidate to me, given your latest description.

    > I read data off an input file into the array.
    You don't have any while (!feof(fp)) loops do you?

    Because this is almost certainly wrong, and frequently leads to an extra element being 'read(*)' from the file. If you sized your arrays to exactly match the file, then you would end up with an array overrun.


    (*) Since it was almost at end of file, what is typically seen is the last data in the file being duplicated.
    Code:
    $ gcc foo.c
    $ ./a.out 
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp = fopen("foo.c","r");
        char buff[BUFSIZ];
        while ( !feof(fp) ) {
            fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
        }
        rewind(fp);
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            fputs(buff,stdout);
        }
        fclose(fp);
        return 0;
    }
    /* the last line */
    /* the last line */
    #include <stdio.h>
    
    int main(void)
    {
        FILE *fp = fopen("foo.c","r");
        char buff[BUFSIZ];
        while ( !feof(fp) ) {
            fgets(buff,BUFSIZ,fp); fputs(buff,stdout);
        }
        rewind(fp);
        while ( fgets(buff,BUFSIZ,fp) != NULL ) {
            fputs(buff,stdout);
        }
        fclose(fp);
        return 0;
    }
    /* the last line */
    As you can see, the broken feof() loop causes the last line of the file to be output twice. The very last fgets() call read no data at all, but left the previous buffer unmodified. Unless you check the fgets() itself for success, you could well end up processing the same buffer again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Differences between int ** var and int var[x][y]
    By Niourf in forum C Programming
    Replies: 5
    Last Post: 10-18-2012, 10:13 PM
  2. Ubuntu
    By GReaper in forum Tech Board
    Replies: 8
    Last Post: 09-06-2010, 10:46 PM
  3. C , C++ , C# differences.
    By mystic in forum Tech Board
    Replies: 15
    Last Post: 07-14-2010, 11:04 PM
  4. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  5. Differences between if() and for()
    By Robert_Sitter in forum C++ Programming
    Replies: 7
    Last Post: 10-31-2005, 10:39 PM

Tags for this Thread