开发者

c++ different error depends on whether or not I'm printing a variable

开发者 https://www.devze.com 2023-03-24 03:00 出处:网络
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 an

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 named bead 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) beads. The lipids 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 picked lipids 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号