Thread: Unexplainable performance loss when printing a variable out of a loop

  1. #16
    Registered User
    Join Date
    Dec 2005
    Posts
    9
    Thank you Salem! Scientific demonstration!
    If you look at the code, consider that it is only a partial version of the full code, which I alredy wrote in fortran (works good), and I'm translating in C because I want to learn it (It's only a week I'm programming in C ... from Christmas...) and because I want to use the program on my MacOsx G4 (I don't have a fortran compiler....too expensive.. :-( ).

  2. #17
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    there is the fortran compiler with the gcc which is invoked by f77 - is that not available with your version of gcc? It appears that it is available for newer versions.
    Last edited by kermit; 12-31-2005 at 12:52 PM.

  3. #18
    Registered User
    Join Date
    Dec 2005
    Posts
    9
    I think there is g77 on MacOsx (as for Linux) but I use fortran90 and I need absoft or ibm fortran 90/95 compiler... (which are hard to find on p2p.... ;-) )
    Which better reason for learning C? :-)
    Bye!

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    GNU seem to have two flavours on my Linux box, so I'd expect similar ports for your machine as well
    g77 (1) - GNU project Fortran 77 compiler
    gfortran (1) - GNU Fortran 95 compiler

    > It's only a week I'm programming in C ... from Christmas...
    You're doing very well then I must say.

    Some comments on your code.
    > main (unsigned int arguments, char *argv[]) {
    1. implicit return types are old, you should really state what the return type of each function is.
    2. The first parameter is conventionally declared int argc
    3. main normally ends with a return 0; if the program completed successfully.

    > checkfiles(argv,arguments);
    Any particular reason for swapping these around compared to the parameters of main(). A minor nit, but every bit of consistency improves readability.
    In addition, you should prototype each function before you try and call it. Say for example
    Code:
    void checkfiles ( char *argv[], int arguments );
    In checkfiles
    > int i;
    > i=2;
    > FILE *fp;
    Standard C does NOT support mixed declarations and statements (the new C99 standard does, as do C++ and extensions to normal C compilers).

    So it would be say
    int i;
    FILE *fp;
    i=2;

    > int calc_type(char *argv[])
    If the code doesn't do the return 0;, then some random value will be returned instead.
    This is very bad for how you may use the result in main with

    > int length_src(char *argv[], int i)
    1. char temp[1];
    This is too small to store any string read by fscanf, which always stores a \0 to mark the end of the string. To store a single letter from the file, you need at least char temp[2] characters. Obviously, if strings are longer in the file, you'll need a larger array here.

    > atom[i][1]=*temp;
    Given the declaration of temp, atom[i][1]=temp[0]; would be more readable.

    > fp=fopen(argv[i],"r");
    You don't call fclose() inside this function - like you do in other functions.

    > big_one_vec()
    > auto float **source,sjkprint;
    The auto keyword is very much a redundant keyword in C. Local variables are auto by default, and there is no other place you can use auto anyway (unlike say static).

    > source=malloc(natoms*sizeof(int));
    You have the wrong sizeof here - it should be sizeof(float*), since source is float**

    Generally speaking, if p is your pointer, then you would say
    p = malloc ( howmany * sizeof *p );

    Probably not yet important, but you don't call free() on any memory you get with malloc(). Memory allocated in main isn't so much a problem as memory allocated in a function like this, especially if the function was being called many times, and that memory was not being freed on each call. This is a memory leak.

    These are the warnings I get when I turn on more compiler diagnostics
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c -lm
    foo.c:15: warning: return type defaults to ‘int’
    foo.c:15: warning: first argument of ‘main’ should be ‘int’
    foo.c: In function ‘main’:
    foo.c:31: warning: implicit declaration of function ‘checkfiles’
    foo.c:33: warning: implicit declaration of function ‘calc_type’
    foo.c:35: warning: implicit declaration of function ‘reduced’
    foo.c:42: warning: implicit declaration of function ‘length_src’
    foo.c:82: warning: implicit declaration of function ‘load_data’
    foo.c:84: warning: implicit declaration of function ‘big_one_vec’
    foo.c:86: warning: implicit declaration of function ‘edpa’
    foo.c: At top level:
    foo.c:93: warning: return type defaults to ‘int’
    foo.c: In function ‘checkfiles’:
    foo.c:96: warning: ISO C90 forbids mixed declarations and code
    foo.c: At top level:
    foo.c:161: warning: return type defaults to ‘int’
    foo.c:257: warning: return type defaults to ‘int’
    foo.c: In function ‘big_one_vec’:
    foo.c:261: warning: unused variable ‘sjkprint’
    foo.c: At top level:
    foo.c:329: warning: return type defaults to ‘int’
    foo.c: In function ‘edpa’:
    foo.c:331: warning: control reaches end of non-void function
    foo.c: In function ‘checkfiles’:
    foo.c:107: warning: control reaches end of non-void function
    foo.c: In function ‘load_data’:
    foo.c:244: warning: control reaches end of non-void function
    foo.c: In function ‘big_one_vec’:
    foo.c:259: warning: ‘j’ may be used uninitialized in this function
    foo.c:327: warning: control reaches end of non-void function
    foo.c: In function ‘main’:
    foo.c:91: warning: control reaches end of non-void function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. appending a int to a variable in a loop?
    By wario in forum C++ Programming
    Replies: 5
    Last Post: 06-28-2003, 11:29 PM
  2. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM
  3. For loop ; printing records
    By Prezo in forum C Programming
    Replies: 3
    Last Post: 09-15-2002, 06:38 AM
  4. printing values loop.
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-02-2002, 02:56 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM