gpt4 book ai didi

c++ - 返回指向 char 数组成员的指针会导致哪些意外行为?

转载 作者:行者123 更新时间:2023-11-30 00:49:57 25 4
gpt4 key购买 nike

好的,就这样。我一直在做一个类(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/

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