Reputation: 21
I feel confused about lambda in c++.Is it related to the compiler? The following code run correct in ubuntu g++ 4.6.3 and g++ 5.2. But when I run it in centos 4.8.5,the result is error.
//
class ScopeGuard
{
public:
explicit ScopeGuard(std::function<void ()> onExitScope)
:onExitScope_(onExitScope)
{
}
~ScopeGuard()
{
onExitScope_();
}
private:
std::function<void ()> onExitScope_;
};
And there is a function to uncompress the data.
//
...
int dstLen = 10 * 1024 * 1024;
char *dstBuf = new char[dstLen];
// When I comment this line the err return Z_OK, otherwise return Z_BUFF_ERROR.
ScopeGuard guard([&](){if (dstBuf) delete[] dstBuf; dstBuf=NULL;});
// zlib function.
int err = uncompress((Bytef *)dstBuf, (uLongf*)&dstLen, (Bytef*)src, fileLen);
if (err != Z_OK)
{
cout<<"uncompress error..."<<err<<endl;
return false;
}`
Upvotes: 0
Views: 83
Reputation: 72215
This is most likely because of this: (uLongf*)&dstLen
.
dstLen
is an int
, which is 32 bits and all current typical systems. uLongf
, however, is an alias for unsigned long
, which is 32 bits on Windows and 32-bit *nix systems, but 64 bits on 64-bit *nix systems.
It is not safe, and likely to do the wrong thing, to cast an int*
to an uLongf*
.
The solution is to make dstLen
an uLongf
and remove the cast.
Upvotes: 1