g++ gone crazy

hi, i've got this situation i cannot explain:
first case, short version:
1
2
3
4
5
6
printf("%d, %d, %d", data[1], data[2], data[3]);

Nz = Nr = 1e5 ;
for(i=1;i<=Nr;i++)
     for(j=1;j<=Nz;j++)
       ro[i][j] = 0.0 ;

it prints out all values for data and then computes ro


second case, short version:
1
2
3
4
5
6
Nz = Nr = 1e5 ;
for(i=1;i<=Nr;i++)
     for(j=1;j<=Nz;j++)
       ro[i][j] = 0.0 ;

printf("%d, %d, %d", data[1], data[2], data[3]);

computes ro and then in prints out only the first value of data, the rest of them are zeroes

this piece of code is part of a greater one which is a CPU and memory consuming ; but the thing is... why does it behave like this ? there are 2 independent operations. Note: data and ro are global variables and these operations are done in main()

My machine is a 64 bit linux and i'm using g++, updated to day.
Are there any parameters to g++ that need to be taken into account ?
Last edited on
I would say you are just doing something very wrong at some other part of your code.
i rectify, Nz = Nr = 1000 ;
Well... why are you using all that C stuff in C++? And it might be that ro and data overlap here, though it's hard to tell without seeing the rest of your code.
i guess it's the stupid memory again... switching to calloc and 1D data...
i am only familiar to C code, and g++ is the only compiler that met my requirements.. gcc is bad... especially when doing this:
gcc fails >> error ; g++ succeeds >> no error
1
2
const unsigned long Nz=1000, Nr=1000;
double ro[Nz][Nr];
Somehow, that doesn't surprise me considering that g++ is a C++ compiler. :/

-Albatross
Actually, g++ should be failing and gcc should succeed. (Although you shouldn't be doing that ever anyway!!)
In the first post shouldn't the loop be:
1
2
for(i=0;i<Nr;i++)
     for(j=0;j<Nz;j++)

For the second problem it's better to use define for that anyway.
1
2
#define Nz 1000
#define Nr 1000 

const is really a C++ feature and isn't supported in C to that degree.
Last edited on
i got lazy dealing with all the warnings and errors when compiling with gcc and seeing that g++ went fine, i chose g++ ...
one of the the errors i encounter when compiling this with gcc :
1
2
3
4
5
struct something {
                               
                                } ;

something *p1, *p2 ;


error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token

am I doing something wrong here ? g++ does not report this error
also, when using p1 and p2 (these are global variables) in other functions located in other files (the files are included after I declare p1 and p2) gcc reports :
1
2
error: ‘p1’ undeclared (first use in this function)
error: ‘p2’ undeclared (first use in this function)
Last edited on
In Standard C AFAIK you cannot just refer to a struct like that. You must either typedef it or refer to it as struct something.

I generally like to use this style:
1
2
3
4
5
typedef struct {
    //...
} something;

//use it as normal 
thanks you. that did the trick.
also, when creating a function with referenced parameteres:
1
2
3
4
void do_it(double &var1, double &var2)
{
   // do some stuff to var1 and var2 and want it saved
}

i get the error : : error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
am i still doing the c++ way although i try to only write in C ?
1
2
3
4
5
6
7
8
void do_it(double &var1, double &var2)
{
   // do some stuff to var1 and var2 and want it saved
}

int main() {
  return 0;
}


Can you show which line of code the error appear ? I tried above and it compiles ok.
in your post it's first line
does it have to do with the fact that the function is in a file with no main() ? (the file is included)
1
2
3
4
#include"some_dir/file_contains_do_it.h"
int main() {
  return 0;
}

the error looks like:
1
2
In file included from main_program.c:54:0:
some_dir/file_contains_do_it.h:19:27: error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
Then have you taken a look at some_dir/file_contains_do_it.h line 19 column 27 ?
There are no references in C, only pointers.
@sohguanh : some_dir/file_contains_do_it.h line 19 column 27 is the & in the front of var1
Topic archived. No new replies allowed.