gpt4 book ai didi

c - VALGRIND MALLOC 和寻找邪恶段错误的探索

转载 作者:行者123 更新时间:2023-12-03 19:10:51 24 4
gpt4 key购买 nike

下面将是我的代码,然后是 Valgrind 的 txt。我不知道为什么它这么长,非常抱歉。我真的很新。请帮助我理解为什么在编译时没有错误消息但程序失败?我知道这是某个地方的内存泄漏,但我不知所措。我应该在下面的困惑中寻找什么?

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// customer record
typedef char customerName[9];

typedef struct customer {
customerName name; // 1-9 upper case letters
int lineNumber; // line number customer gets on
int time; // arrival time at line
int numberItems; // number of items customer has
} customer;

// [singly linked] queue element
typedef customer *qitem;

typedef struct node {
qitem data; // pointer to actual data
struct node *next; // forward pointer to next item
} node;

// queue definition (singly linked list)
// NOTE:
typedef struct queue {
node *front; // pointer to first node in list ie front of line with cashier
} queue;

// qinit -- initialize/reset queue
void qinit(queue *q)
{
q->front = NULL;
}

// qempty -- returns 1 if empty or 0 if false
int qempty(queue *q)
{
return (q->front == NULL);
}

// enqueue -- append element to end of queue
void enqueue(queue *q, qitem data)
{
node *newnode;
node *prev;

newnode = malloc(sizeof(node));
newnode->next = NULL;
newnode->data = data;

// find the back of the queue with only a front pointer
prev = NULL;
for (node *cur = q->front; cur != NULL; cur = cur->next)
prev = cur;

// append to tail of list
if (prev != NULL)
prev->next = newnode;

// add to end of empty list
else
q->front = newnode;

}

// dequeue -- dequeue from the front of the queue
qitem dequeue(queue *q)
{
node *curnode;
qitem data;

do {
curnode = q->front;

// bug out if list is empty
if (curnode == NULL) {
data = NULL;
break;
}

// get node's data value (e.g. pointer to customer struct)
data = curnode->data;

// release the node's storage back to the heap
free(curnode);
} while (0);

return data;
}

// qfront -- peek at front of queue
qitem qfront(queue *q)
{
node *curnode;
qitem data;

curnode = q->front;
if (curnode != NULL)
data = curnode->data;
else
data = NULL;

return data;
}


int main(int argc, const char * argv[]) {

int testCases = 0;
scanf("%d", &testCases);

if(testCases > 0 && testCases <= 25){//shortcircuiting???

while (testCases--){

queue *q;
q = malloc(sizeof(queue));
qinit(q);// starting new queue

int numCustomers;
scanf("%d", &numCustomers);

if(numCustomers < 0 || numCustomers > 11){
return 0;
}

queue* customerArray = (queue*) malloc(sizeof(queue) * 12);

for ( int i = 0; i < numCustomers; i++){

customer* newCustomer = (customer*) malloc(sizeof(customer));

scanf("%d", &(newCustomer->time));
scanf("%d", &(newCustomer->lineNumber));
scanf("%s", newCustomer->name);
scanf("%d", &(newCustomer->numberItems));
enqueue(&customerArray[newCustomer->lineNumber - 1], newCustomer);
}


int totalTime = INT_MAX;

for(int i=0;i<12;i++)
{
customer* frontCustomer = qfront(&customerArray[i]);
if(totalTime < frontCustomer->time)
{
totalTime = frontCustomer->time;
}
free(frontCustomer);
}

while(numCustomers--) {

int customerToCheckOutLine = 0; int minNumberOfItems = INT_MAX;

for( int j=11 ; j>=0; j--){

customer* frontCustomer = qfront(&customerArray[j]);
if(frontCustomer->time <= totalTime)
{
if(frontCustomer->numberItems < minNumberOfItems)
{
customerToCheckOutLine = frontCustomer->lineNumber;
minNumberOfItems = frontCustomer->numberItems;
}
free(frontCustomer);
}
}


customer* customerToCheckOut = qfront(&customerArray[customerToCheckOutLine -1 ]);
totalTime += 30;
totalTime += (customerToCheckOut->numberItems) * 5;
dequeue(&customerArray[customerToCheckOutLine - 1]);
}
free(customerArray);

}
}
return 0;
}


样本输入

2
5
10 1 史蒂文 12
12 6 艾哈迈德 8
13 1 珍妮 40
22 6 杰梅因 39
100000 12 阿玛利亚 53
6
100 1 A 100
200 2 乙 99
300 3 C 98
400 4 D 97
500 5 东 96
600 6 华氏度 95

最佳答案

到达这条线时

    // release the node's storage back to the heap
free(curnode);

您在队列中有一个指向 currnode 的实时指针.下次您调用队列函数时,它将尊重该指针。需要在 dequeue()中调整队列的内容.

关于c - VALGRIND MALLOC 和寻找邪恶段错误的探索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62249511/

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