违规阅读地点 [英] violation reading location

查看:91
本文介绍了违规阅读地点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于在课堂上提出的公共信息问题而删除了代码。

code removed due to public information issues brought up in class.

推荐答案

在第一个代码段中, strcpy(node->名称,名称)导致访问冲突,因为节点不包含任何有效内存位置的地址。





再看看你的作业代码表明你的代码中有几个flawas:



1)保存和加载在您的实现中不是对称的:保存尝试先存储秒,然后加载时尝试首先加载名称,然后加载秒。

2)你似乎将sizeof()与strlen()混淆为名称。

3)如果名称总是如此,您只能使用 fread 来读取名称固定长度。

4)为什么要将数据存储为二进制文件?为什么不发短信(保存: fprintf(fp,%f%s \ n,节点 - >第二,节点 - >名称); ,加载: fscanf(fp,%f%s \ n,& seconds,buffer); )。

5)为什么不使用 add 函数在 loading 功能?

6)加载功能坏了:你试图从文件中读取所有记录,但不要添加每条记录 while循环中。

7)在 save 中,不要关闭所有控制分支中的文件 - 移动它来自 if 块。

8)你不一致地使用 list_head :一次你修改它并使其无效(例如在 save 函数中,而在 load 函数中你不重置它在加载之前。

9)请尝试从职业生涯开始时给出含义名称: temp ,简单或任何组合形式,都是废话。如果你没有给出一个像样的名字,你可能还没有理解手头的任务。为什么不给它命名 node_to_add 或类似的?



[/ EDIT1]



干杯

Andi
In the first code snippet, strcpy(node->name,name) leads to an access violation since node does not hold the address of any valid memory location.


Looking again at your homework code shows that you have several flawas in your code:

1) Saving and loading are not symetric in your implementation: save tries to store first the seconds and only then the name while loading tries to load first the name and then the seconds.
2) You seem to confuse sizeof() with strlen() for the name.
3) You can only use fread for reading the name if the name has always a fixed length.
4) Why do you store the data as binary file? Why not text (save: fprintf(fp, "%f %s\n", node->second, node->name);, load: fscanf(fp, "%f %s\n", &seconds, buffer);).
5) Why don't you employ the add function to add a node in the load function?
6) The load function is broken: you try to read all records from the file, but don't add each record within the while loop.
7) In save, you do not close the file in all control branches - move it out of the if block.
8) You use the list_head inconsistently: once you modify it and invalidate it as such (e.g. in the save function, while in the load function you do not reset it before loading.
9) Please try to give meaningul names from the beginning of your career: temp, plain or in any combined form, is crap. If you fail to give a decent name you probably have not understood the task at hand. Why don't you name it node_to_add or alike?

[/EDIT1]

Cheers
Andi


这篇关于违规阅读地点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆