这个stl代码崩溃,为什么??? [英] this stl code crashes, why???

查看:60
本文介绍了这个stl代码崩溃,为什么???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




全局运算符new和delete都重载了我正在使用stl map

来存储指针,但是这段代码崩溃了,可以有一个流了一些

光???

编译:MS VC ++ 6.0

STL:随Visual Studio一起发货。


#include< malloc.h>

#include< map>


使用命名空间std;

typedef map< void *,void *>指针地图;


指针地图gMemStore;


void * operator new(size_t size)

{

void * p = :: malloc(size);

gMemStore [p] = p;

返回p;

}


void operator delete(void * p)

{

PointersMap :: iterator it = gMemStore.find(p);

if(it!= gMemStore.end())

gMemStore.erase(it);

:: free(p); < br $>
}


int main(int argc,char * argv [])

{

return 0;

}


TIA。

-Paul。

解决方案
"保罗" < BG ***** @ yahoo.com>在消息中写道

news:d8 ************************** @ posting.google.c om ...



全局运算符new和delete都被重载了,我正在使用stl map来存储指针,但是这个代码崩溃了,有些人会掉一些<编辑:MS VC ++ 6.0
STL:随Visual Studio一起提供。

#include< malloc.h>
#include< ; map>

使用命名空间std;
typedef map< void *,void *> PointersMap;

PointersMap gMemStore;

void * operator new(size_t size)
{
void * p = :: malloc(size);
gMemStore [p] = p;
返回p;
}
void operator delete(void * p)
{PointersMap :: iterator it = gMemStore.find(p);
if(it!= gMemStore.end())
gMemStore.erase(it);
:: free(p);
}

int main(int argc,char * argv [])
{
返回0;
}



嗯,我可以看到这可能会导致无限递归。您的运营商

新调用std :: map< void *,void *> :: operator []可以调用operator new,

调用std :: map< void *,void *> :: operator [],它可以调用operator

new等等。这只是猜测,因为我没有VC ++ 6。你有没有考虑使用调试器
?它可能会给你一个比这个新组更简单的b $ b b直接答案。


-

David Hilsee


* Paul:


全局运算符new和delete都重载了我正在使用stl map
存储指针,但这段代码崩溃了


应该读取:因此此代码崩溃。


尝试搜索无限递归。


int main(int argc,char * argv [])
{
返回0;
}




这不会运用代码,似乎没有用处。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:什么是最烦人的事情usenet和电子邮件?




" Paul" < BG ***** @ yahoo.com>在消息中写道

news:d8 ************************** @ posting.google.c om ...



全局运算符new和delete都被重载了,我正在使用stl map来存储指针,但是这个代码崩溃了,有些人会掉一些<编辑:MS VC ++ 6.0
STL:随Visual Studio一起提供。

#include< malloc.h>
#include< ; map>

使用命名空间std;
typedef map< void *,void *> PointersMap;

PointersMap gMemStore;

void * operator new(size_t size)
{
void * p = :: malloc(size);
gMemStore [p] = p;
返回p;
}
void operator delete(void * p)
{PointersMap :: iterator it = gMemStore.find(p);
if(it!= gMemStore.end())
gMemStore.erase(it);
:: free(p);
}

int main(int argc,char * argv [])
{
返回0;
}

TIA。< br-> -Paul。




您是否因堆栈空间不足而崩溃?

问题可能正如其他人所说的那样gMemStore [p]调用new,

给出一个无限循环。

你可以创建自己的命名空间来修复它,所以你可以使用f.ex.

app :: new和app :: delete for your own code。


PKH


Hi,

Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with Visual Studio.

#include <malloc.h>
#include <map>

using namespace std;
typedef map<void*,void*> PointersMap;

PointersMap gMemStore;

void* operator new(size_t size)
{
void * p = ::malloc(size);
gMemStore[p] = p;
return p;
}

void operator delete(void* p)
{
PointersMap::iterator it = gMemStore.find(p);
if(it != gMemStore.end())
gMemStore.erase(it);
::free(p);
}

int main(int argc, char* argv[])
{
return 0;
}

TIA.
-Paul.

解决方案

"Paul" <bg*****@yahoo.com> wrote in message
news:d8**************************@posting.google.c om...

Hi,

Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with Visual Studio.

#include <malloc.h>
#include <map>

using namespace std;
typedef map<void*,void*> PointersMap;

PointersMap gMemStore;

void* operator new(size_t size)
{
void * p = ::malloc(size);
gMemStore[p] = p;
return p;
}

void operator delete(void* p)
{
PointersMap::iterator it = gMemStore.find(p);
if(it != gMemStore.end())
gMemStore.erase(it);
::free(p);
}

int main(int argc, char* argv[])
{
return 0;
}



Well, I could see how this might cause infinite recursion. Your operator
new calls std::map<void*,void*>::operator[] which could call operator new,
which calls std::map<void*,void*>::operator[], which could call operator
new, etc. That''s just a guess, because I do not have VC++6. Have you
considered using a debugger? It would probably give you a more
straightforward answer than this newgroup.

--
David Hilsee


* Paul:


Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes
That should read: and therefore this code crashes.

Try to search for "infinite recursion".

int main(int argc, char* argv[])
{
return 0;
}



This does not exercise the code and seems to serve no purpose.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?



"Paul" <bg*****@yahoo.com> wrote in message
news:d8**************************@posting.google.c om...

Hi,

Global operator new and delete are overloaded and I am using stl map
to store pointers, but this code crashes, can some one shed some
light???
Compiler: MS VC++ 6.0
STL: Shipped with Visual Studio.

#include <malloc.h>
#include <map>

using namespace std;
typedef map<void*,void*> PointersMap;

PointersMap gMemStore;

void* operator new(size_t size)
{
void * p = ::malloc(size);
gMemStore[p] = p;
return p;
}

void operator delete(void* p)
{
PointersMap::iterator it = gMemStore.find(p);
if(it != gMemStore.end())
gMemStore.erase(it);
::free(p);
}

int main(int argc, char* argv[])
{
return 0;
}

TIA.
-Paul.



Do you crash with an out of stack-space message ?
The problem is probably as others have said that gMemStore[p] calls new,
giving an infinite loop.
You could create your own namespace to fix it, so you could use f.ex.
app::new and app::delete for you own code.

PKH


这篇关于这个stl代码崩溃,为什么???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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