- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,就这样。我一直在做一个类(class)项目(我们还没有涉及 std::string 和 std::vector 但显然我知道它们)来构建各种时钟。该程序的主要部分需要时间和日期值作为格式化的 c 字符串(例如“12:45:45”、“12/12/12”等),我可能可以通过将它们存储相同来保持简单在我的基础课上。但是,我没有。
相反,我这样做了:
class UsageEntry {
public:
....
typedef time_t TimeType;
typedef int IDType;
...
// none of these getters are thread safe
// furthermore, the char* the getters return should be used immediately
// and then discarded: its contents will be modified on the next call
// to any of these functions.
const char* getUserID();
const char* getDate();
const char* getTimeIn();
const char* getTimeOut();
private:
IDType m_id;
TimeType m_timeIn;
TimeType m_timeOut;
char m_buf[LEN_MAX];
};
其中一个 setter/getter (它们基本上都做同样的事情):
const char* UsageEntry::getDate()
{
strftime(m_buf, LEN_OF_DATE, "%D", localtime(&m_timeIn));
return m_buf;
}
这是一个使用这个指针的函数:
// ==== TDataSet::writeOut ====================================================
// writes an entry to the output file
void TDataSet::writeOut(int index, FILE* outFile)
{
// because of the m_buf kludge, this cannot be a single
// call to fprintf
fprintf(outFile, "%s,", m_data[index].getUserID());
fprintf(outFile, "%s,", m_data[index].getDate());
fprintf(outFile, "%s,", m_data[index].getTimeIn());
fprintf(outFile, "%s\n", m_data[index].getTimeOut());
fflush(outFile);
} // end of TDataSet::writeOut
这会造成多大的麻烦?或者换个角度看,还有什么有趣的!!FUN!!行为会导致这种情况吗?最后,可以做些什么来修复它(除了使用字符串/vector 的明显解决方案之外)?
有点相关:做类似事情的 C++ 库函数如何处理这个问题?例如localtime() 返回一个指向 struct tm 对象的指针,该对象以某种方式在该函数调用结束后存活至少足够长的时间以供 strftime 使用。
最佳答案
没有足够的信息来确定它是否会造成麻烦,因为你没有展示你如何使用它。只要您记录注意事项并在使用类(class)时牢记它们,就不会有问题。
有一些常见问题需要注意,但希望这些是常识:
删除 UsageEntry
将使您的 getter 返回的指针无效,因为这些缓冲区也将被删除。 (如果使用本地声明的 UsageEntry
,如 MadScienceDream's example 中,这将特别容易遇到。)如果存在风险,调用者应创建自己的字符串拷贝。记录下来。
m_timeIn
看起来不像是const
,因此它可能会改变。调用 getter 将修改内部缓冲区,并且这些更改对任何具有该指针的对象都是可见的。如果存在风险,调用者应创建自己的字符串拷贝。记录下来。
您的 getter 既不是可重入的也不是线程安全的。记录下来。
让调用者提供目标缓冲区和长度作为参数会更安全。为方便起见,该函数可以返回指向该缓冲区的指针。这就是例如read
有效。
强大的 API 可以避免问题。如果做不到这一点,良好的文档和常识也可以减少出现问题的可能性。行为只有在没有人预料到的情况下才是意外的,这就是关于行为的文档很重要的原因:它通常可以消除意外行为。
可以把它想象成 toastr 顶部的“小心:高温表面”警告。您可以设计顶部带有绝缘层的电 toastr ,这样就不会发生事故。如果做不到这一点,您至少可以在上面贴上警告标签,可能不会发生事故。如果既没有绝缘也没有警告,最终有人会自焚。
现在您已经编辑了您的问题以在标题中显示一些文档,许多初始风险已经降低。这是一个很好的改变。
这是一个示例,说明如果使用用户提供的缓冲区(并返回指向该缓冲区的指针),您的用法将如何改变:
// ==== TDataSet::writeOut ====================================================
// writes an entry to the output file
void TDataSet::writeOut(int index, FILE* outFile)
{
char userId[LEN_MAX], date[LEN_MAX], timeIn[LEN_MAX], timeOut[LEN_MAX];
fprintf(outFile, "%s,%s,%s,%s\n",
m_data[index].getUserID(userId, sizeof(userId)),
m_data[index].getDate(date, sizeof(date)),
m_data[index].getTimeIn(timeIn, sizeof(timeIn)),
m_data[index].getTimeOut(timeOut, sizeof(timeOut))
);
fflush(outFile);
} // end of TDataSet::writeOut
关于c++ - 返回指向 char 数组成员的指针会导致哪些意外行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26593547/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!