Preface
This is probably a bad question but I'm truly hopeless here. I know that I my question is a bad one since it is highly specific to my problem but I'll try to describe the problem the best and most general way I can...Some background
In my code, I have a data structure namedbead
which holds, among other an integer variable named LID
. I开发者_StackOverflow also have a data structure named lipid
which holds, among others, three pointers to (three different) bead
s. The lipid
s are held in a vector<lipid*>
variable named lipids
and bead
's LID
variable is equal to the position of the appropriate lipid
in lipids
+1.
During my (Monte Carlo - MC) simulation, I choose a random number between 0 and 999 and change appropriate lipid
. I then test the change using a function named calcEnergy
which accept the lipid
's bead
pointers one at a time as it's input (named mb
).
The problem
I got a bad allocation error after I have pickedlipids
position number 261. I have tried to find why does that happen so I have typed:
if(mb->LID ==261){
printf("something");
}
when I tried this, The simulation stopped (segmentation fault) when the program have picked lipids
position number 132. So, I tried to see why is that and printed to the screen the LID
of each bead
I have sent using printf()
. Now the program survive the 132 lipid
and crushes on the next lipid
.
I am absolutely clueless about the cause of why does the program crashes when I try to read a variable which I know it's valid.
Once again, I know this is a long shot but I have now other idea...
Thanks.
I would guess that a function is taking in a lipid * and overwriting one of its bead pointers, so that later, it reads an invalid pointer which contains invalid bead data.
Try editing your "calc energy" function to not do anything, just take in the pointer and return. See if that stops the bug. If it does, add one line of code at a time until you find the line that causes the error.
精彩评论