CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   One dimensional array in OF21(Segmentation fault (core dumped)) (https://www.cfd-online.com/Forums/openfoam-programming-development/246759-one-dimensional-array-of21-segmentation-fault-core-dumped.html)

farzadmech December 21, 2022 14:03

One dimensional array in OF21(Segmentation fault (core dumped))
 
Hello all
I am defining a one dimensional parameter which has 2 million elements;
Code:

  int nCounter = U.size();
  Info << "1" << endl;
  scalar AAAA[nCounter];
  for(int i=0; i<nCounter; i++) { AAAA[i] = 0;}

and it gives this error;
Code:

1
Segmentation fault (core dumped)

why I got this error in OF21?

farzadmech December 21, 2022 19:45

I afraid that maybe i counter goes beyond the limits so I change it to
Code:

  for(int i=1 i<100; i++) { AAAA[i] = 0;}
but still it gives me the same error. Why?

Thanks,
Farzad


Quote:

Originally Posted by farzadmech (Post 841591)
Hello all
I am defining a one dimensional parameter which has 2 million elements;
Code:

  int nCounter = U.size();
  Info << "1" << endl;
  scalar AAAA[nCounter];
  for(int i=0; i<nCounter; i++) { AAAA[i] = 0;}

and it gives this error;
Code:

1
Segmentation fault (core dumped)

why I got this error in OF21?


olesen December 22, 2022 05:00

I don't understand the source of your error, but there is no reason to be using raw C arrays instead of an OpenFOAM list or even a std vector. Give the following a try:
Code:

scalarField AAA(U.size());

for (label i=0; i < AAA.size(); ++i)
{
    AAA[i] = 0;
}

If you actually want to initialize with zero (and not just demonstrate the segfault), it would actually make sense to do that immediately on construction:
Code:

scalarField AAA(U.size(), Zero);

farzadmech December 22, 2022 13:22

Thanks a lot for your advice. I did it like this as you said and it works;

Code:

  int nCounter = U.size();
  scalarField AAA(nCounter);


and since I need it to set zero every time step, I set it zero like this every time step;

Code:


for(int i=0; i<nCounter; i++)
{AAA[i] = 0;}

Is there any easier way to put it zero every time step?


Thanks,
Farzad



Quote:

Originally Posted by olesen (Post 841623)
I don't understand the source of your error, but there is no reason to be using raw C arrays instead of an OpenFOAM list or even a std vector. Give the following a try:
Code:

scalarField AAA(U.size());

for (label i=0; i < AAA.size(); ++i)
{
    AAA[i] = 0;
}

If you actually want to initialize with zero (and not just demonstrate the segfault), it would actually make sense to do that immediately on construction:
Code:

scalarField AAA(U.size(), Zero);


überschwupper December 23, 2022 01:50

Quote:

Originally Posted by farzadmech (Post 841663)
Thanks a lot for your advice. I did it like this as you said and it works;

Code:

  int nCounter = U.size();
  scalarField AAA(nCounter);

and since I need it to set zero every time step, I set it zero like this every time step;

Code:


for(int i=0; i<nCounter; i++)
{AAA[i] = 0;}

Is there any easier way to put it zero every time step?


Thanks,
Farzad


You can either use the vectorized method to make the for loop obsolete (but I think the compiler automatically using SIMD and might be that an overloaded operator[] will prohibit this operation) or I know for the GeometricField<T> class there is an overloaded operator= which sets the whole Field with a particular value. Since GeometricField is inhereting from Field class (if you go for Field<scalar>) I think there should be something similiar



Code:

AAA[:] = 0 //vectorized

// works at least for GeometricField<T>
AAA = dimensionedScalar(dimless, 0.0) //sets .ref and .boundaryFieldRed to 0



All times are GMT -4. The time now is 17:29.