CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

One dimensional array in OF21(Segmentation fault (core dumped))

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 21, 2022, 14:03
Default One dimensional array in OF21(Segmentation fault (core dumped))
  #1
Senior Member
 
Farzad Faraji
Join Date: Nov 2019
Posts: 204
Rep Power: 7
farzadmech is on a distinguished road
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 is offline   Reply With Quote

Old   December 21, 2022, 19:45
Default
  #2
Senior Member
 
Farzad Faraji
Join Date: Nov 2019
Posts: 204
Rep Power: 7
farzadmech is on a distinguished road
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 View Post
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 is offline   Reply With Quote

Old   December 22, 2022, 05:00
Default
  #3
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,685
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
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 likes this.
olesen is offline   Reply With Quote

Old   December 22, 2022, 13:22
Default
  #4
Senior Member
 
Farzad Faraji
Join Date: Nov 2019
Posts: 204
Rep Power: 7
farzadmech is on a distinguished road
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 View Post
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 is offline   Reply With Quote

Old   December 23, 2022, 01:50
Default
  #5
Member
 
Join Date: Jan 2022
Location: Germany
Posts: 72
Rep Power: 4
überschwupper is on a distinguished road
Quote:
Originally Posted by farzadmech View Post
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
überschwupper is offline   Reply With Quote

Reply

Tags
array, error, fault, of21, segmentation


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OpenFOAM] Paraview segmentation fault core dumped GLfast80 ParaView 0 March 13, 2021 12:53
Segmentation fault (core dumped) prashantgoel CONVERGE 2 May 26, 2020 12:48
SU2-7.0.1 on ubuntu 18.04 hyunko SU2 Installation 7 March 16, 2020 04:37
Segmentation fault (core dumped) Sugajen OpenFOAM Running, Solving & CFD 1 August 2, 2017 12:06
solving a conduction problem in FLUENT using UDF Avin2407 Fluent UDF and Scheme Programming 1 March 13, 2015 02:02


All times are GMT -4. The time now is 20:55.