严重记忆冲突:变量在C中冲突 [英] Serious Memory Clash: Variables clashing in C
问题描述
struct消息
{
char type;
double idNum;
char *时间;
char *资产;
bool BS;
float price1;
int shares1;
float price2;
int shares2;
};
typedef结构消息消息;
struct Asset
{
oBook * OrderBook;
Trade * TradeBook; //将指向最新的交易
int QtyTraded;
float ValueTraded;
char *时间;
};
typedef struct Asset Asset;
int main(int argc,char * argv [])
{
Message * newMessage;
资产*支票;
//检查的操作和初始化,以便它保存适当的值。
newMessage = parser(N,2376,01 / 02/2011 09:15:01.342,JPASSOCIAT FUTSTK 24FEB2011,B,84.05,2000,0,0,newMessage);
// MessageProcess(newMessage,AssetMap);
printf(最后交易地址%p最后交易时间%s \ n,Check-> TradeBook,Check->时间);
Message * parser(char * message,Message * new_Message)
{
char a [9] [256];
char * tmp = message;
bool inQuote = 0;
int counter = 0;
int counter2 = 0;
new_Message =(Message *)malloc(sizeof(Message));
while(* tmp!='\0')
{
switch(* tmp)
{
case',':if(!inQuote)
{
a [counter] [counter2] ='\0';
counter ++;
counter2 = 0;
}
break;
case'':
inQuote =!inQuote;
break;
默认值:
a [counter] [counter2] = * tmp;
counter2 ++;
break;
}
tmp ++;
}
a [counter] [counter2] ='\0';
new_Message-> ; type = * a [0];
new_Message-> Time =& a [2] [11];
new_Message-> asset = a [3];
if * a [4] =='S')
new_Message-> BS = 0;
else
new_Message-> BS = 1;
new_Message-> price1 = atof(a [5]);
new_Message-> shares1 = atol(a [6]);
new_Message-> price2 = atof(a [7]);
new_Message- > shares2 = atol(a [8]);
new_Message-> idNum = atoi(a [1]);
return(new_Message);
}
它们都是不同类型的结构我试图解决这个问题,因为当解析器改变 请建议如何解决这个问题。我花了10个小时左右的时间,并计算解决这个问题,并吨头发。 编辑添加STRUCTURE DEF 您正在使用堆栈分配对象的地址初始化new_Message-> Time =& a [2] [11]; Here there is a serious memory clash, in two variables of different scope. I have investigated using gdb and it seems the address of They both are structures of different types I am trying to resolve this issue, because, when parser changes the value of Please do suggest how to solve this problem. I have lost(spent) around 10 hours and counting to resolve this issue, and tons of hair. Soham EDIT STRUCTURE DEF ADDED You're using an address of the stack allocated object to initialize new_Message->Time = &a[2][11]; 这篇关于严重记忆冲突:变量在C中冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<这里有一个严重的记忆冲突,有两个不同范围的变量。我调查过使用gdb,它的地址似乎是 new_Message-> Time
的地址等于 Check-> Time $ c的地址$ C>。
new_Message->时间
它操纵检查 - >时间的内容
Soham
struct Message
{
char type;
double idNum;
char *Time;
char *asset;
bool BS;
float price1;
int shares1;
float price2;
int shares2;
};
typedef struct Message Message;
struct Asset
{
oBook *OrderBook;
Trade *TradeBook; //will point to the latest trade
int QtyTraded;
float ValueTraded;
char* Time;
};
typedef struct Asset Asset;
int main(int argc, char*argv[])
{
Message* newMessage;
Asset* Check;
//manipulation and initialization of Check, so that it holds proper values.
newMessage = parser("N,2376,01/02/2011 09:15:01.342,JPASSOCIAT FUTSTK 24FEB2011,B,84.05,2000,0,0",newMessage);
// MessageProcess(newMessage,AssetMap);
printf("LAST TRADE ADDRESS %p LAST TRADE TIME %s\n",Check->TradeBook,Check->Time);
}
Message* parser(char *message,Message* new_Message)
{
char a[9][256];
char* tmp =message;
bool inQuote=0;
int counter=0;
int counter2=0;
new_Message = (Message*)malloc(sizeof(Message));
while(*tmp!='\0')
{
switch(*tmp)
{
case ',': if(!inQuote)
{
a[counter][counter2]='\0';
counter++;
counter2=0;
}
break;
case '"':
inQuote=!inQuote;
break;
default:
a[counter][counter2]=*tmp;
counter2++;
break;
}
tmp++;
}
a[counter][counter2]='\0';
new_Message->type = *a[0];
new_Message->Time = &a[2][11];
new_Message->asset = a[3];
if(*a[4]=='S')
new_Message->BS = 0;
else
new_Message->BS = 1;
new_Message->price1=atof(a[5]);
new_Message->shares1=atol(a[6]);
new_Message->price2=atof(a[7]);
new_Message->shares2=atol(a[8]);
new_Message->idNum = atoi(a[1]);
return(new_Message);
}
new_Message->Time
is equalling to the address of Check->Time
. new_Message->Time
it manipulates the contents of Check->Time