- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为此工作了几个小时。我完全被难住了。
这是 CS113 的实验室。
如果用户在程序(二进制计算器)结束时选择继续,我们需要使用 goto 语句来到达程序的顶部。
但是,我们还需要释放所有分配的内存。
这两件事似乎是不相容的。如果我释放分配的内存并且用户选择继续,那么一切都会变得非常疯狂。即使这些值被重新初始化,但在这个过程中也会以某种方式搞砸。我真的很抱歉,但我不知道如何更好地解释。尽管步骤与第一次运行相同,但它们最终似乎充满了垃圾。
我的代码如下。请注意,“释放分配的内存”部分在我的版本中已被注释掉,但我将其留在这里,以免造成混淆。
编辑:现在可以释放CPU。目前的版本可以正常工作。但是,我无法释放我分配的字符串或由双向链表组成的寄存器。我无法使用的免费函数已被注释掉,并在程序的末尾。
注意:如果内存在最后没有释放,程序也绝对可以正常运行,即使多次运行也是如此。请注意这一点。
#include
#include
#include
#include"lab9.h"
#include
int chính()
{
struct cpu_t *cpu = NULL;
top:
if(cpu)
{
free(cpu);
cpu = NULL;
}
cpu = malloc(sizeof(struct cpu_t));
if(!cpu) /* Error check malloc for CPU */
{
printf("Sorry! Malloc failed to allocate space for the CPU!\n");
thoát(THOÁT_THẤT_CẦN);
}
struct bit_t *temp1 = cpu->r1_head;
struct bit_t *temp2 = cpu->r2_head;
struct bit_t *temp3 = cpu->r3_head;
/* Setting all flags to 0 by default */
cpu->overflow = 0;
cpu->carry = 0;
cpu->sign = 0;
cpu->parity = 0;
cpu->zero = 0;
char buffer[128];
char unsign; /* Holds a char: 0 for signed, 1 for unsigned */
char *expression = NULL; /* Binary expression */
char *e1 = NULL; /* Holds the first part of the expression */
char op; /* Holds the operand */
char *e2 = NULL; /* Holds the second part of the expression */
char ans; /* y if user wants to continue, else ends program */
int temp = 0; /* Used for printing separating dashes */
int one_count = 0; /* Number of ones in the result */
printf("\nPlease enter the word size: ");
fgets(buffer, sizeof(buffer), stdin);
cpu->word_size = atoi(buffer); /* atoi takes a pointer, don't dereference buffer*/
while(cpu->word_size < 1 || cpu->word_size > 64) /* Error check word size */
{
printf("Error: Word size must be between 1 and 64. \n");
printf("Enter a new word size: \n");
fgets(buffer, sizeof(buffer), stdin);
cpu->word_size = atoi(buffer);
}
printf("Are the values unsigned? [Y/N]: ");
unsign = *fgets(buffer, sizeof(buffer), stdin); /* fgets returns address to data, must be dereferenced */
switch(unsign)
{
case 'y':
case 'Y':
cpu->unsign = 1;
phá vỡ;
case 'n':
case 'N':
cpu->unsign = 0;
phá vỡ;
}
printf("Please enter the binary expression: ");
expression = fgets(buffer, sizeof(buffer), stdin); /* Stores expression in string */
e1 = strtok(expression, " "); /* Breaks off first number*/
switch(check_string(cpu, e1)) /* Error check first string */
{
case 1:
printf("Error in input: Length of operand greater than word size. \n");
printf("Error in first operand. Retry. \n");
goto top;
case 2:
printf("Error in input: Something other than a 1 or 0 entered. \n");
printf("Error in first operand. Retry. \n");
goto top;
mặc định:
phá vỡ;
}
e1 = zero_pad(cpu, e1); /* Zero pads first number */
op = *strtok(NULL, " "); /* Operation (+, -, ^, &, |) */
e2 = strtok(NULL, " \n"); /* Original string had \n at end. Otherwise length would be 1 over actual. */
switch(check_string(cpu, e2)) /* Error check second string */
{
case 1:
printf("Error in input: Length of operand greater than word size. \n");
printf("Error in second operand. Retry. \n");
goto top;
case 2:
printf("Error in input: Something other than a 1 or 0 entered. \n");
printf("Error in second operand. Retry. \n");
goto top;
mặc định:
phá vỡ;
}
e2 = zero_pad(cpu, e2); /* Zero pads second number */
create_r1(cpu, e1);
create_r2(cpu, e2);
create_r3(cpu);
switch(op)
{
case '+':
add_function(cpu);
phá vỡ;
case '-':
op = '+';
complement(cpu); /* Turns second expression into 2's complement of itself */
add_function(cpu);
phá vỡ;
case '&':
and_function(cpu);
phá vỡ;
case '|':
or_function(cpu);
phá vỡ;
case '^':
xor_function(cpu);
phá vỡ;
mặc định:
printf("Error in operator. Please retry. \n");
goto top;
}
temp1 = cpu->r1_head;
temp2 = cpu->r2_head;
temp3 = cpu->r3_head;
/* Print the first register */
while(temp1)
{
printf("%d", temp1->n);
temp1 = temp1->next;
}
puts("");
/* Print operator */
printf("%c\n", op);
/* Print second register */
while(temp2)
{
printf("%d", temp2->n);
temp2 = temp2->next;
}
puts("");
/* Print spacing dashes */
while(temp < (cpu->word_size))
{
printf("-");
temp++;
}
puts("");
/* Print third register (stores answer) */
while(temp3)
{
printf("%d", temp3->n);
temp3 = temp3->next;
}
puts("");
/* Flag handling */
/* Reset temp variables */
temp1 = cpu->r1_head;
temp2 = cpu->r2_head;
temp3 = cpu->r3_head;
/* Flag handling */
if(temp3->n == 1)
{
cpu->sign = 1;
}
while(temp3)
{
if(temp3->n == 1)
{
one_count++;
}
temp3 = temp3->next;
}
if(one_count == 0)
{
cpu->zero = 1;
}
else if(one_count % 2 == 0)
{
cpu->parity = 1;
}
printf("Flags: \n");
printf("Overflow: %d \n", cpu->overflow);
printf("Carry: %d \n", cpu->carry);
printf("Sign: %d \n", cpu->sign);
printf("Parity: %d \n", cpu->parity);
printf("Zero: %d \n", cpu->zero);
printf("Decimal: %d \n", find_decimal(cpu));
printf("Do you want to continue? [Y/N] \n"); /* If not 'y' or 'Y', close program */
ans = *fgets(buffer, sizeof(buffer), stdin);
/* Reset temps...again */
temp1 = cpu->r1_head;
temp2 = cpu->r2_head;
temp3 = cpu->r3_head;
/* Free all allocated memory */
//delete_list(cpu->r1_head);
//delete_list(cpu->r2_head);
//delete_list(cpu->r3_head);
/* Free zero_string */
//free(e1);
//free(e2);
if(ans == 'Y' || ans == 'y')
{
temp = 0;
goto top;
}
trả về 0;
}
1 Câu trả lời
也许您可以将 main 的开头更改为如下所示:
int chính() {
struct cpu_t *cpu = NULL;
top:
if (cpu) {
free(cpu);
cpu = NULL;
}
cpu = malloc(sizeof(struct cpu_t));
关于无法 malloc 然后转到程序顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33928635/
我已经在标准 WPF 控件中实现了一个报告,并且还实现了一个 DocumentPaginator获取这些控件并将它们转换为用于打印的文档。 我还实现了一些使用文档分页器将页面呈现为图像并使用 PDFS
在 C# 中,我有以下代码: public static string GetHashCode(string p) { var a = new SHA256Managed();
您好,我正在尝试在编码后将我的 mysqli 数据库输出到一个 js 文件,我用 json_encode 对其进行编码没有任何问题,但是如何将其放入 js 文件中(每次更新时更新) mysqli数据已
我需要将 select 从 JS 传递到 HTML。 select 应该包含来自 PHP 的 option。 所以,首先我有一个 HTML div,我将在其中添加来自 JS 的内容。
我有一个相当大且复杂的 SVG 代码,它根据页面信息使用 JavaScript 和 jQuery 动态生成。 然后我有一个 AJAX 帖子保存。 我无法将其转换为正确发布图像数据? var canva
我想将我的本地日期 ([NSDate date]) 转换为 GMT 以创建一个 JSON 字符串 (/Date(1324435876019-0000)/)。 当我将时钟设置为 EST 时区时,我的代码
1. 原始单据与实体之间的关系 可以是一对1、一对多、多对多的关系。在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体。在特殊情况下,它们可能是一对多或多对一的关系,即一张原
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章服务器五大相关基础知识【转】由作者收集整理,如果你对这篇文章有兴趣,记得
Google Apps 脚本 - Gmail 是否会实现 GmailMessage (GmailThread) .getAsPdf() 方法?预期输出与 Gmail 中可用的打印为 PDF 的输出相同
有一个需求是要在一个云监控的状态值中存储多个状态(包括可同时存在的各种异常、警告状态)使用了位运算机制在一个int型中存储。 现在监控日志数据量非常大(亿级别)需要对数据按每小时、每天进行聚合,供
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章1张图看懂RAID功能,6张图教会配置服务器【转】由作者收集整理,如果你
我正在使用 FFMPeg(版本 ffmpeg-20170330-ad7aff0-win64-static)将 RTSP 转换为 .m3u8。 命令是: ffmpeg -rtsp_transport t
我有一个 JTree使用 DefaultTreeModel 的对象作为模型,我添加/删除与该模型相关的节点。 此时,我需要在图形界面中显示树结构,例如 JPanel .如何映射 DefaultTree
我当前正在接收一个文件并将其存储到 NSString 中。然后,我从字符串中创建一个数组并将其呈现在 TableView 中。这在一定程度上有效。我目前收到的数据如下: 公司名称|帐户代码\r\n公司
我需要创建 NSImage cocoa 对象的 base64 字符串表示形式。处理这个问题的最佳方法是什么,苹果文档似乎在这个主题上有点短(或者我只是找不到它)。 Base64 编码从外面看起来相当复
JS 中的 .toISOString() 函数给我这样的字符串: 2015-06-14T20:00:00:000Z 我需要它是这样的: 2015-06-14T20:00:00Z JS 中是否有其他函数
我正在尝试使用 JavaScript 转换 COLORREF: COLORREF : When specifying an explicit RGB color, the COLORREF value
我在这里遇到了这个代码的问题,只是想制作一个小计算器: 打包申请; import javafx.event.ActionEvent; import javafx.scene.control.TextF
我想要做的是能够通过本地PC上的USS通过sshfs挂载主机上的一些文件。我可以做到这一点,但 sshfs 不能直接完成从 EBCDIC 到 ascii/unicode 的转换。有没有我可以设置的标志
我正在尝试在 python 中将一堆 Visio 文件转换为 pdf。我已经引用了这个.doc to pdf using python并编写了以下代码: import comtypes.client
Tôi là một lập trình viên xuất sắc, rất giỏi!