C++ 分段错误:STL映射的意外行为
我在代码中使用STL映射。其中一个函数是使用全局声明的映射,它给出分段错误。但是如果我把这个映射作为局部变量,它就可以正常工作了。下面是函数创建问题C++ 分段错误:STL映射的意外行为,c++,map,C++,Map,我在代码中使用STL映射。其中一个函数是使用全局声明的映射,它给出分段错误。但是如果我把这个映射作为局部变量,它就可以正常工作了。下面是函数创建问题 typedef map<int,string> DebugAllocPtrList_t; DebugAllocPtrList_t DebugAllocPtrList; int g_DebugAllocCount; void DebugAllocNew(void *x, char *szFile, int iLine
typedef map<int,string> DebugAllocPtrList_t;
DebugAllocPtrList_t DebugAllocPtrList;
int g_DebugAllocCount;
void DebugAllocNew(void *x, char *szFile, int iLine)
{
char szBuf[512];
szBuf[0]=0;
sprintf(szBuf,"%s line %d", szFile, iLine);
printf("Memory already allocated");
DebugAllocPtrList[(int)x] = szBuf; //here it gives fault when declared globally.
g_DebugAllocCount++;
}
typedef映射DebugAllocPtrList\t;
DebugAllocPtrList\u t DebugAllocPtrList;
int g_debugallocount;
void DebugAllocNew(void*x,char*szFile,int-iLine)
{
char-szBuf[512];
szBuf[0]=0;
sprintf(szBuf,“%s行%d”,szFile,iLine);
printf(“已分配内存”);
DebugAllocPtrList[(int)x]=szBuf;//这里全局声明时给出错误。
g_DebugAllocCount++;
}
如果我独立运行这个函数,它就会工作。但是当我把这个函数放到我的实际代码中时
它给出了分段错误。如果我生成DebugAllocPtrList\t DebugAllocPtrList变量局部,然后我也会工作 > P> >我想将您的代码重新写入下面的代码,没有混合C/C++代码,没有全局变量等,更多的C++方式:
typedef map<int,string> DebugAllocPtrList_t;
void incrementCount()
{
static int g_DebugAllocCount = 0;
g_DebugAllocCount++;
}
std::string makeString(const std::string& file, int line)
{
std::stringstream ss;
ss << file << " line " << line;
return ss.str();
}
void DebugAllocNew(DebugAllocPtrList_t& ptr_map, int x, const std::string& file, int line)
{
std::cout <<"Memory already allocated" <<std::endl;
ptr_map[x] = makeString(file, line);
incrementCount();
}
int main()
{
DebugAllocPtrList_t t;
DebugAllocNew(t, "something", 1, 2);
DebugAllocNew(t, "something", 1, 2);
}
typedef映射DebugAllocPtrList\t;
void incrementCount()
{
静态int g_DebugAllocCount=0;
g_DebugAllocCount++;
}
std::string makeString(const std::string&file,int行)
{
std::stringstream-ss;
SS< P>我想把你的代码改写到下面的代码,没有混合C/C++代码,没有全局变量等,更多的C++方式:
typedef map<int,string> DebugAllocPtrList_t;
void incrementCount()
{
static int g_DebugAllocCount = 0;
g_DebugAllocCount++;
}
std::string makeString(const std::string& file, int line)
{
std::stringstream ss;
ss << file << " line " << line;
return ss.str();
}
void DebugAllocNew(DebugAllocPtrList_t& ptr_map, int x, const std::string& file, int line)
{
std::cout <<"Memory already allocated" <<std::endl;
ptr_map[x] = makeString(file, line);
incrementCount();
}
int main()
{
DebugAllocPtrList_t t;
DebugAllocNew(t, "something", 1, 2);
DebugAllocNew(t, "something", 1, 2);
}
typedef映射DebugAllocPtrList\t;
void incrementCount()
{
静态int g_DebugAllocCount=0;
g_DebugAllocCount++;
}
std::string makeString(const std::string&file,int行)
{
std::stringstream-ss;
ssDebugAllocPtrList
local to what?toDebugAllocNew
函数?但在这种情况下,它总是在作用域出口处被破坏…请澄清。请尝试提供一个演示错误的最小示例。实际上,这是我传递整数或某个时间浮点的某个时间,但这不会产生问题,因为相同的函数如果我将DebugAllocPtrList_t DebugAllocPtrList变量声明为同一个函数的局部变量,则n可以正常工作。如果您对szBuf[0]的意思是这样的,则使用:char szBuf[512]={'\0'}=0;当它实际上是一个映射时,为什么称它为一个列表?DebugAllocPtrList
local to what?toDebugAllocNew
函数?但在这种情况下,它总是在作用域出口处被销毁…请澄清。请尝试提供一个演示错误的最小示例。实际上,这是我传递整数或某个时间floa的某个时间但这并没有产生问题,因为如果我将DebugAllocPtrList_t DebugAllocPtrList变量声明为同一个函数的局部变量,那么同一个函数也可以正常工作。使用:char szBuf[512]={'\0'}如果这就是您对szBuf[0]=0的意思,那么当它实际上是一个映射时,为什么要称它为列表呢?谢谢您的建议……谢谢您的建议。。。。