gpt4 book ai didi

C 指针段错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:07 26 4
gpt4 key购买 nike

我正在做一个 C 项目。唯一的好处是我们有一个健康重要的阅读计划。我可以添加患者并添加读数并删除患者。我有所有其他工作,其中我将链接我的代码。我有一个我在 removePatient 中隔离的设置错误。我试过 gdb,但出于某种原因,它今天不想和我一起工作。

这里是有问题的代码:

void removePatient(int patientID) {
int i, count;
Chartptr patientsChart;
Chartptr previousChart;
Chartptr currentChart;
CBuffptr healthTypeBuffer;
CBuffptr allHealthTypeBuffers[MAXREADINGS];

// if patient was found, remove the patient
patientsChart = getChart(patientID);
if (patientsChart != NULL) {
healthTypeBuffer = patientsChart->buffer;

if (healthTypeBuffer != NULL) {
// gather all the heath type buffers
count = 0;
for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {
allHealthTypeBuffers[i] = healthTypeBuffer;
healthTypeBuffer = healthTypeBuffer->next;
count++;
}

// free all the health type buffers
for (i = 0; i < count; ++i) {
free(allHealthTypeBuffers[i]);
}
}

// find the chart before specified patient chart
currentChart = patientList;
while (currentChart != patientsChart) {
previousChart = currentChart;
currentChart = currentChart->next;
}

// reorganize list, then free patient chart
previousChart->next = patientsChart->next;
free(patientsChart);
}
}

我相信我写的代码是相当可读的。

下面是上面代码中使用的一些结构声明:

/* One health type reading: timestamp + actual value */
typedef struct{
char timestamp[MAXTIME+1];
int value;
}Element;

/*
* Health type readings: linked list of Circular buffers
*/
typedef struct healthEntry* CBuffptr; /* pointer to a CircularBuffer */

typedef struct healthEntry{
int type; /* health data type (1-5) */
int start; /* index of oldest reading */
int end; /* index of most current reading */
Element reading[MAXREADINGS]; /* fixed array of readings */
CBuffptr next; /* pointer to next health type buffer */
}CircularBuffer;

/*
* Patient's health chart: ID + linked list of health type readings
*/
typedef struct chartEntry* Chartptr; /* pointer to a Chart */

typedef struct chartEntry{
int id; /* patient ID */
CBuffptr buffer; /* pointer to first health type buffer */
Chartptr next; /* pointer to next patient */
}Chart;

/* global declaration for start of the patient chart linked list */
extern Chartptr patientList;

最佳答案

虽然我没有仔细阅读您的大部分代码,但以下行对我来说似乎很可疑:

for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {

我怀疑你希望它是:

for (i = 0; i < MAXREADINGS && healthTypeBuffer != NULL; ++i) {

可能还有其他问题,但我很确定上面的逻辑至少调用了&&

关于C 指针段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20463420/

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