- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面将是我的代码,然后是 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;
}
最佳答案
到达这条线时
// release the node's storage back to the heap
free(curnode);
currnode
的实时指针.下次您调用队列函数时,它将尊重该指针。需要在
dequeue()
中调整队列的内容.
关于c - VALGRIND MALLOC 和寻找邪恶段错误的探索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62249511/
接上篇 通过一个示例形象地理解C# async await 非并行异步、并行异步、并行异步的并发量控制 前些天写了两篇关于C# async await异步的博客, 第一篇博客看的人多,点
前言 在 SwiftUI 中,我们可以通过添加不同的交互来使我们的应用程序更具交互性,这些交互可以响应我们的点击,点击和滑动。 今天,我们将回顾SwiftUI基本手势:
今年我一直在想,2022年我想做些什么,做哪方面的改变,这周末在家终于想到了! 2021 轻描淡写 年底就一直想对2021年写一篇总结的,起码不得写个千八百字,可是思来想去不知道怎么写,直到最后都没想
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
在 Eclipse 中使用 Java 进行开发时,它非常方便:您可以像自己一样附加源代码并探索核心 Java 代码。在 Visual Studio 中,我知道只有在调试时才能查看 .net 源代码(我
我正在尝试创建自己的字符串数据类型,谁能告诉我 typedef 和初始化做错了什么。 #include #include typedef char string[10]; int main(){
我期待开发一些东西来分析在服务器上运行的应用程序的 JVM 线程,要求如下: 访问在单独应用程序中运行的所有线程 打印线程栈 了解事件的详细信息 - 记录执行时间和方法详细信息(在特定线程中执行) 我
是否可以探索 Android 内部存储?我需要这个用于调试目的,以帮助我的开发工作。 最佳答案 您可以在模拟器上,或在 Root设备上。只是 adb shell 连接设备,然后从那里导航。 关于and
我有一个使用大量外键的 innoDB 表,但我们只想从中查找一些基本信息。 我做了一些研究,但还是迷路了。 如何判断我的主机是否有 Sphinx已经安装了吗?我没看到作为表格存储的选项方法(即 inn
我有一个创建列表的 GWT 代码(作为结果的网格),我将样式设置为 CSS 类,如 .test tr { height: 26px; } 现在...如果在渲染未完成或网格没有元素时我需要从代码
我需要使用 Javascript 和 HTML 为 Rally 敏捷工具开发一个 View 。我没有处理过在我作为开发人员的新职业中经常使用的网络语言。 我只是在探索他们的 API,但不知道如何探索他
我想了解 Hadoop 而不是一个黑盒子。我想探索 Hadoop 代码本身。我怎样才能不从主干下载 bundle ,我应该从哪里开始?任何帮助都会很有帮助谢谢舒佳特 最佳答案 Hadoop 代码在 S
想象一下这样的情况。您获得了一些遗留代码或获得了一些新框架。您需要尽快调查并了解如何使用此代码。没有机会向以前的开发人员寻求帮助。什么是最佳实践/方法/方式/步骤/工具(首选 .NET Framewo
我注意到我的 git 存储库中的某些 makefile 缺少变量定义的问题,我想搜索所有提交历史以查找我的变量 TESTDIR 在变更集中出现的位置 我该怎么做? 干杯 最佳答案 你可以使用 git
有什么方法可以探索 GO 包吗? 在 java 中,我使用“javap java.lang.String”命令来查看类内部的方法。像这样,有没有命令是他们用 GO 语言写的? 我在谷歌中搜索了相同的内
我注意到 docker 我需要了解容器内发生了什么或其中存在哪些文件。一个示例是从 docker 索引下载图像 - 您不知道图像包含什么,因此无法启动应用程序。 理想的情况是能够通过 ss
近日,华为 分析服务 6.9.0版本发布,正式上线 探索能力 。开发者可自由定义与配置分析模型,支持报告实时预览,数据洞察体验更加灵活与便捷. 新上线的探索能力中,有漏斗分析、事件归因、会话路径分析
我有一个 4 列的 excel 2010 电子表格。 A 列:我销售的产品的 UPC 代码列表。大约300行。 B 列:公式(稍后会详细介绍) C 列:另一个 UPC 代码列表。这些 UPC 代码大约
我有 3 个表格如下: CREATE TABLE USER_STATUS ("UID" varchar2(7), "STAT_ID" varchar2(11)) ; INSERT ALL IN
有什么方法可以探索 java 脚本对象(如 telerik 菜单或任何其他第 3 方对象)的属性和/或功能?我可以通过调试和破坏然后在 watch 中添加对象或在 VS 中使用智能感知来实现。 我使用
我是一名优秀的程序员,十分优秀!