Find the answer to your Linux question:
Results 1 to 8 of 8
Hi guys, I have a bit strange problem while compiling and running some c++ codes on linux, that I'd appreciate if someone helps me with. It is the exact same ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    14

    Error while compiling a C++ code on linux, but it compiles and runs!

    Hi guys,

    I have a bit strange problem while compiling and running some c++ codes on linux, that I'd appreciate if someone helps me with. It is the exact same problem each time. So, I'll describe it for one of the codes I have:

    In that code, my .cpp file is called averagemprograms.cpp and it contains the following:


    Code:
    #include <string>
    #include <cstdlib>
    using namespace std;
    #include <fstream>
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <cstdlib>
    #include <math.h>
    using namespace std;
    
    
    
    
    int main (void)
    {
    int tmp;
    int s;
    int count;
    
    char output[100];
    char numString[100];
    float myVal[1000];
    for (int oo=0;oo<50;oo++){
    strcpy(output,"output");
    sprintf(numString,"%d",oo);
    strcat(output,numString);
    strcat(output,".dat");
    
    FILE *file;
    file=fopen(output,"r");
    
    	if(!file)
    	{
    		cout<<"No File";
    		return 0;
    	}
    
    	else {
    
    
    	 char c[100];  
    	 char val[50];
    	 int L;
    	
        
    	 
        for(int i=0;i<13;i++)
    		fgets(c, 100, file);
    
    	L=strlen(c);	
    	for(int i=27;i<L;i++)
    		val[i-27]=c[i];
    	val[L-27-1]=NULL;
    	printf("\n");
    	sscanf(val,"%f",&myVal[oo]);
    	printf("%0.5f",myVal[oo]);
    	printf("\n");
    	fclose(file);
      }
    
    
    }
    double sum=0;
    ofstream fout("objective function statistics for the M programs.txt");
    double min=10000000000;
    double max=0;
    for (int ttt=0;ttt<50;ttt++){
    	fout<<myVal[ttt];
    	sum=myVal[ttt]+sum;
    	fout<<"\n";
    	if (myVal[ttt]<min){
    			min=myVal[ttt];
    		}
    		if(myVal[ttt]>max){
    			max=myVal[ttt];
    		}
    }
    
    fout<<"\n\n\n";
    double average=sum/50;
    fout<<"Average Objective Function Value of the M programs="<<average;
    fout<<"\n";
    fout<<"Maximum="<<max;
    fout<<"\n";
    fout<<"Minimum="<<min;
    double difference[50];
    double sum2=0;
    
    for (int gg=0;gg<50;gg++){
    	difference[gg]=pow((myVal[gg]-average),2);
    		sum2=difference[gg]+sum2;
    		
    }
    
    
    double stdev=0;
    stdev=sum2/(49*50);
    fout<<"\n\n";
    fout<<"Variance of the M programs="<<stdev;
    fout<<"\n";
    int kkk=sqrt(stdev);
    fout<<"Standard deviation of the M programs="<<kkk;
    return 0;
    }

    In which I read each of 50 files (named output0 through output49), take a certain value from each (that value starts at the exact same place in each file), and then using the 50 values, I do some statistics on them (calculate the average, standard deviation, maximum and minimum). And finally, I output those statistics in a .txt file.

    I compile the previous .cpp file using:

    Code:
    g++ -g averagemprograms.cpp -o myprogram
    It gives the following error and 2 warnings:

    Code:
    averagemprograms.cpp: In function ‘int main()’:
    averagemprograms.cpp:55: warning: converting to non-pointer type ‘char’ from NULL
    averagemprograms.cpp:103: warning: converting to ‘int’ from ‘double’
    N.B: I copied and pasted the previous error and warnings as is from the screen, with the same weird characters that you guys can see! Also, I use SecureCRT to connect to the unix maching of our department, on which I run all my codes!


    Anyways, although I get the previous error, it gives the compiled file myprogram which runs perfectly when I run it using ./myprogram.

    So, my questions are:

    First, how could it compile and gives a correct compiled file that runs perfectly although it has an error?

    And second, how can I correct that error and those 2 warnings?

    Thanks in advance!

    Aly

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Given those 3 lines of output, there is no error. The first line tells you the function where the warnings occur.

  3. #3
    Just Joined!
    Join Date
    May 2009
    Posts
    14
    Thanks a lot, Coopstah.

    I understand what you said, but what are those strange characters?

    And do you have an idea what those warnings mean? and/or how they can be prevented?

    Also, I noticed that sometimes I have something in my code that should prevent it from being compiled, but it doesn't. More specifically, I might declare an array of integers as follows:

    int tyty[10]

    And then I do a specific loop in my code, from 0 to 19 to fill tyty. Such a thing in windows won't compile (because it will simply not find tyty[11] or tyty[12],...etc and so on in order to fill. But under linux, it compiles, and it created tyty[11], tyty[12],...etc!!!!! What is the logic here?

    That is not in the code in my original post, but I did it in a different code, and it compiled, and just had similar warnings to the code in the previous post!

    Any help will be appreciated!

    Aly

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    the function sqrt returns a double, but you are assigning an int its value, you should either cast it as an int, or change it to a double

    for the pointer issue, i believe you would solve it by changing
    Code:
    char val[50];
    to
    Code:
    char *val = new char[50];
    been a while since I have done c++though

  5. #5
    Just Joined!
    Join Date
    May 2009
    Posts
    14
    Thanks a lot, man. I'll try that.

    But what about this?

    Quote Originally Posted by amegahed3 View Post
    Also, I noticed that sometimes I have something in my code that should prevent it from being compiled, but it doesn't. More specifically, I might declare an array of integers as follows:

    int tyty[10]

    And then I do a specific loop in my code, from 0 to 19 to fill tyty. Such a thing in windows won't compile (because it will simply not find tyty[11] or tyty[12],...etc and so on in order to fill. But under linux, it compiles, and it created tyty[11], tyty[12],...etc!!!!! What is the logic here?

    That is not in the code in my original post, but I did it in a different code, and it compiled, and just had similar warnings to the code in the previous post!

    Any help will be appreciated!

    Aly

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    if you have something like
    Code:
    int intArray[10];
    for (int i = 0; i < 20; i++) {
     intArray[i] = i;
    }
    This will compile, but should segfault at runtime. It should not create or expand the array. Show me your exact code.

  7. #7
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    The line: val[L-27-1]=NULL;
    is invalid. NULL is defined as a (char*) and not char, but you are assigning to a char (array member of a char[]). Use the value 0 instead.

    The line: int kkk=sqrt(stdev);
    is incorrect. The sqrt() function returns a floating point value, but you are assigning it to an integer. You either need to cast the results, or assign it to a variable of the proper type.

    I don't know what the wierd characters are, but the compiler probably is outputting something that it things should be interpreted differently by the tty driver for the terminal window you are running in. Don't worry. It is just cosmetic.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  8. #8
    Just Joined!
    Join Date
    Apr 2009
    Location
    Berkeley, California
    Posts
    20
    You have an odd mix of C/C++ libraries, as well as C and C++ code. As noted earlier, your compiler is giving you warnings, not errors. You will definitely run into problems later on if, for example, you expected a certain level of precision from your square root operation. Try
    intptr_t x = static_cast<intptr_t>(sqrt(y));
    Also, you can easily overrun the bounds of an array, but again, you will most likely encounter errors or a segmentation fault if you try to access those values later in the program or operation. The fact that the compiler compiles the overrun has nothing to do with Windows or Linux, but rather with the compiler. You would be better off using STL containers, such as vectors, so that you don't make inadvertent errors like that.
    Gotta love C++; I do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...