gpt4 book ai didi

c++ - 将数组传递给 C++ 中的函数会更改它的值,而无需我在函数内部执行任何更改

转载 作者:行者123 更新时间:2023-11-30 00:38:28 28 4
gpt4 key购买 nike

我在使用 C++ 数组时遇到了一个非常奇怪的问题。一切都很好,直到我将它传递给一个函数以将其写入磁盘,之后,数组被更改而无需我对其执行任何操作。

    int saveMatrix(Long64_t*, unsigned int, const char*);

void resetArray(Long64_t* array, unsigned int size)
{
for (unsigned long int i = 0; i < size; i++)
array[i] = 0;
}

int saveMatrix(Long64_t* array, unsigned int size, const char* filename)
{
// Returns:
// 0 - Exit
// 1 - Error

ofstream out(filename, ios::out);
if (!out) {
cout << "Cannot open file.";
return 1;
}

for (unsigned long int i = 0; i < size; i++) {
out << array[i] << " ";
}

out.close();

return 0;
}

int main(int argc, char** argv) {
if (argc != 2) {
cout << "The program needs the name of the ROOT file to analyse." << endl;
return 1;
}
else
return gf_partial(argv[1]);
}
#endif

#include "../include/ROOT_Tree.hh"
#include "jbmArray.hh"

int gf_partial(const char* name) {
// init macro
gROOT->Reset();

unsigned long int i, j;

const unsigned int DetectorNumber = 4;
/* Detectors are:
0 lefs60Foil
1 lefs60Fl
2 lefs60Ml
3 lefs150Foil
4 lefs150F
5 lefs150M
6 ca60D
7 ca60C
8 ca60B
*/
const double sourceRadius = 20.; // LAN2B In cm
// const double sourceRadius = 11.; // LAN2A In cm
const Int_t numberOfChannels = 4;
// Float_t energyChannels[numberOfChannels] = {52., 85., 141., 227.}; // LAN2A F electron
Float_t energyChannels[numberOfChannels] = {51., 80., 135., 218.}; // LAN2B F electron
// Float_t energyChannels[numberOfChannels] = {42., 55., 130., 220.}; // LAN2B B electron
// Float_t energyChannels[numberOfChannels] = {66., 101., 167., 269., 447., 798., 1391., 2926.}; // LAN2B M proton
const Int_t numberOfDetectors = 9;
Float_t energyChannelsError[numberOfChannels] = {0., 0., 0., 0.};
// Float_t energyChannelsError[numberOfChannels] = {0., 0., 0., 0., 0., 0., 0., 0.};

Float_t vGeometryFactors[numberOfChannels];
Float_t vError[numberOfChannels]; // the error amount
Float_t vRealError[numberOfChannels]; // the calculated error

Long64_t nHitsInDetector[numberOfChannels];
resetArray(nHitsInDetector, numberOfChannels);

Long64_t nHitsInDetectorNoSecondaries[numberOfChannels];
resetArray(nHitsInDetectorNoSecondaries, numberOfChannels);

double nEmitedParticlesPerChannel[numberOfChannels];
resetArray(nEmitedParticlesPerChannel, numberOfChannels);

//exclusiongraph()->Update();

if (!TClassTable::GetDict("Event")) {
gSystem->Load("../lib/libROOT_Tree.so");
}

// read the tree file generated in GEANT4
TFile* treeFile = new TFile(name);
treeFile->ls();
TTree *myTree = (TTree*) treeFile->Get("hiscaleSim");

// Create pointer to an event object for reading the branch values.
Event* myEvent = new Event();
TBranch* bEvent = myTree->GetBranch("EventBranch");
bEvent->SetAddress(&myEvent);

TClonesArray* hits = myEvent->GetHits();
Hit* myHit;

unsigned long int nEvents = myTree->GetEntries();
cout << endl << "Number of Total Events = " << nEvents << endl << endl;

Float_t sourceEnergy = 0.0;
Float_t depositedEnergy = 0.0;
Float_t sourceTime = 0.0;
UInt_t parentID = 0;
UInt_t trackID = 0;

std::vector<float> fSourcePosition(3);
std::vector<float> minSourcePosition(3);
std::vector<float> maxSourcePosition(3);

std::vector<float> fSourceMomentumDirection(3);
std::vector<float> minSourceMomentumDirection(3);
std::vector<float> maxSourceMomentumDirection(3);

std::vector<float> totalDepositedEnergy(numberOfDetectors);
//std::vector<unsigned long int> nHitsInDetector(numberOfDetectors);

// std::vector<unsigned long int> nHitsInDetectorNoSecondaries(numberOfChannels);

Float_t fParticlePDGMass = 0.0;
Float_t fParticlePDGCharge = 0.0;
//fParticleAtomicNumber = 0;
//fParticleAtomicMass = 0;

for (i = 0; i < nEvents; i++) {
myTree->GetEntry(i);

depositedEnergy = 0;
sourceEnergy = myEvent->GetSourceEnergy();
for (j = 0; j < myEvent->GetNHit(); j++) {
myHit = (Hit*)hits->UncheckedAt(j);
if (myHit->GetDetectorN() == DetectorNumber)
depositedEnergy = myHit->GetTotalDepositedEnergy();
}

parentID = myEvent->GetParentID();
trackID = myEvent->GetTrackID();

for (j = 0; j < numberOfChannels; j++) {
if (sourceEnergy == energyChannels[j]) {
nEmitedParticlesPerChannel[j]++;
if (depositedEnergy != 0) {
if (parentID == 0)
nHitsInDetectorNoSecondaries[j]++;
nHitsInDetector[j]++;
}
}
}
}

char strnHitsInDetector[80];
strcpy (strnHitsInDetector, name);
strcat (strnHitsInDetector, ".nHitsInDetector");
saveMatrix(nHitsInDetector, numberOfChannels, strnHitsInDetector);

//char strnHitsInDetector[80];
strcpy (strnHitsInDetector, name);
strcat (strnHitsInDetector, ".nHitsInDetectorNoSecondaries");
saveMatrix(nHitsInDetectorNoSecondaries, numberOfChannels, strnHitsInDetector);

char strnEmitedParticlesPerChannel[80];
strcpy (strnEmitedParticlesPerChannel, name);
strcat (strnEmitedParticlesPerChannel, ".nEmitedParticlesPerChannel");
saveMatrix(nEmitedParticlesPerChannel, numberOfChannels, strnEmitedParticlesPerChannel);

// from now on nEmitedParticlesPerChannel changes it's value and it's not ok anymore.

最佳答案

我认为问题出在这些代码行中:

const Int_t numberOfChannels = 2;

Long64_t nEmitedParticlesPerChannel[numberOfChannels];
resetArray(nEmitedParticlesPerChannel, numberOfChannels);

nEmitedParticlesPerChannel[1] = 10;
nEmitedParticlesPerChannel[2] = 20;

请注意,您已将数组 nEmittedParticlesPerChannel 的大小设置为包含两个元素,然后写入索引 1 和 2。由于 C++ 数组是零索引的,这意味着您正在注销数组的末尾并破坏内存中恰好紧随其后的任何值。这可能会破坏您作为参数传入的数组的内容,因为这会导致未定义的行为。

要解决这个问题,请尝试将最后两行重写为

nEmitedParticlesPerChannel[0] = 10;
nEmitedParticlesPerChannel[1] = 20;

希望这对您有所帮助!

关于c++ - 将数组传递给 C++ 中的函数会更改它的值,而无需我在函数内部执行任何更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10838041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com