CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   PtrList initialization problem (https://www.cfd-online.com/Forums/openfoam-solving/59030-ptrlist-initialization-problem.html)

mateusz_slowikowski March 13, 2008 15:20

I'm having trouble initializin
 
I'm having trouble initializing some pointers. The code compiles fine, but when I run my case (which, at the moment is very basic) and run my createFields.H file, it says that I'm trying to assign a value to a null reference: is there a way of initializing the pointer list to non-null values?

This is part of my createFields.H file:

const N_Bins = 6;
PtrList<volvectorfield> U_;
U_.setSize(N_Bins);

{
std::stringstream converter;
for (int i = 0; i < N_Bins; i++)
{
converter.str("");
converter << (i+1);
word phaseNumber = word(converter.str());

volVectorField Utemp(
IOobject
(
"Ua"
mesh.time().timeName(),
mesh,
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE
),
Ua
);

U_[i] = Utemp;
}
}

This is the error that is generated when I run the case:

--> FOAM FATAL ERROR : hanging pointer, cannot dereference#0 Foam::error::printStack(Foam:http://www.cfd-online.com/OpenFOAM_D...part/proud.gifstream&)
#1 Foam::error::abort()
#2 main
#3 __libc_start_main
#4 __gxx_personality_v0


From function PtrList::operator[]
in file /usr/local/OpenFOAM/OpenFOAM-1.4/src/OpenFOAM/lnInclude/PtrListI.H at line 92.

philippose March 13, 2008 15:44

Hi, Just a thought... maybe
 
Hi,

Just a thought... maybe PtrList starts from index "1" and not index "0".... not sure, but that error message seems to kind of point to that.

Have a nice day!

Philippose

mateusz_slowikowski March 14, 2008 08:53

Philippose, I've tried maki
 
Philippose,

I've tried making a loop from i=1 -> N_Bins, but I get the same error:

--> FOAM FATAL ERROR : hanging pointer, cannot dereference#0 Foam::error::printStack(Foam:http://www.cfd-online.com/OpenFOAM_D...part/proud.gifstream&)
#1 Foam::error::abort()
#2 main
#3 __libc_start_main
#4 __gxx_personality_v0


From function PtrList::operator[]
in file /usr/local/OpenFOAM/OpenFOAM-1.4/src/OpenFOAM/lnInclude/PtrListI.H at line 92.

I have a suspision that it's making a list of pointers of null values - and I tried to make a pointer array of volVectorFields, declare the loop, then pass the variables into the PtrList, but this also did not work. Thanks for the input either way - it was a worthy idea to check.

francesco_b March 14, 2008 09:14

Hi Mateusz, maybe if you tr
 
Hi Mateusz,

maybe if you try to do make the loop from i=0 to N_binc-1 you'll fix the issue, I don't know if this is the problem, but it seems something about 0 value

Hope this helps

Regards

Francesco

philippose March 14, 2008 13:31

Hi again, I was checking up
 
Hi again,

I was checking up in the Doxygen help system online, and found the following....

T & operator[](const label)[inline]

Return element reference.


Now.... the operator [] returns a reference to the element.... which means, it can be used on the RHS of an assignment... not on the LHS.

As far as I can see, I don't think you can use this to actually assign elements to the pointer list... you need to use the following member function of the PtrList class:

U_.set(i,Utemp)

"set" is the member function which actually lets you fill the list.

Hope this helps :-)!

Philippose

mateusz_slowikowski March 14, 2008 23:27

Actually... this is exactly wh
 
Actually... this is exactly what I was looking for - I changed the code and the program (with fixing some other... elementary... errors) ran perfectly. Thank you for the help!

mikeP February 22, 2012 12:50

Hello, I have a problem on this issue.

It works well for me. I use the volVectorField's in equations using U_[0], U_[1], etc.
However, when the runtime is complete, it does not output the resulting vectorfields although it is mentioned AUTO_WRITE.

why would that be the case?

hismother March 19, 2012 07:09

Hi MikeP,

I am having the same problem that you mentioned. Have you figured out why it is like that?

If yes, could you show me how to solve this problem.

Terrence.

l_r_mcglashan March 19, 2012 14:32

Can you post up your code and a case if possible?


All times are GMT -4. The time now is 11:48.