与零长度参数一起使用的 memset:忽略还是当心? [英] memset used with zero length parameter: ignore or watch out?
问题描述
我在链接动态库时无法找到有关以下警告的任何信息:
I'm having trouble finding any information regarding the following warning when linking a dynamic library:
In function `MyClass::myfunc()':
MyClass.cpp:(.text+0x14e4): warning: memset used with constant zero length parameter; this could be due to transposed parameters
这是myfunc
的摘录:
void MyClass::myfunc() {
vector<Variable*>::const_iterator it;
for (it = m_vars.begin();
it != m_vars.end();
++it) {
if ((*it)->recordme) {
MyRecord* r = new MyRecord(*it);
initMyRecord(*r);
m_records.push_back(r);
}
}
}
所以我很困惑我是否应该为这个 memset 寻找可能的原因.给新接线员的电话是我的第一个嫌疑人,但我什至不确定是否值得寻找这个.我不确定我是应该认真对待这个警告还是让它过去.
So I'm pretty much stuck on were should I be looking for possible causes for this memset. The call to the new operator is my first suspect, but I'm not even sure if it's worth looking for this. I'm not sure either if I should take this warning seriously or let it pass.
问题:我应该如何处理这个警告?我应该注意什么样的模式,以确保我以后不会用脚开枪?
Question: what should I do about this warning? And what kind of patterns should I look out for in order to assure that I'm not going to shoot myself in the foot later?
更新:这是 MyRecord 构造函数,它位于头文件中,因此如果我理解正确,它可能会或可能不会被内联.
Update: Here is the MyRecord constructor, which is in a header file, so it might or might not be inlined, if I understand correctly.
class MyRecord {
public:
MyRecord(const Variable* var) :
buffer(0),
lastSave(-1 * std::numeric_limits<double>::max()),
sample(100),
bufsize(100),
gv(var),
rec_function(0)
{};
virtual ~Record() {
if (rec_function)
delete rec_function;
rec_function = 0;
};
private:
Record(const Record&);
Record& operator=(const Record& rec);
public: // @todo: remove publicness
boost::circular_buffer< boost::tuple<double,boost::any> > buffer;
double lastSave;
double sample;
unsigned int bufsize;
const Variable* gv;
RecordFunctor* rec_function;
};
RecordFunctor 是一个纯虚拟结构:
The RecordFunctor is a pure-virtual struct:
struct RecordFunctor {
virtual ~RecordFunctor() {};
virtual void record(const double) = 0;
};
其他信息?我正在编译标志 -O2
和 g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Additional info? I'm compiling with flags -O2
and g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
推荐答案
您正在调用 boost::circular_buffer
构造函数,capacity
为 0.这可能会导致该构造函数调用 memset()
初始化循环缓冲区使用的存储,但您已经告诉它您想要一个零大小的缓冲区.
You are calling the boost::circular_buffer
constructor with a capacity
of 0. This probably causes that constructor to call memset()
to initialise the storage used by the circular buffer, but you've told it you want a buffer of zero size.
解决方案是为 circular_buffer
构造函数提供您想要的实际大小,而不是零(零没有意义).
The solution is to give the circular_buffer
constructor the actual size you want, not zero (zero doesn't make sense).
这篇关于与零长度参数一起使用的 memset:忽略还是当心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!