gpt4 book ai didi

c - 严重内存冲突: Variables clashing in C

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:14 27 4
gpt4 key购买 nike

    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);
}

这里有一个严重的内存冲突,在两个不同范围的变量中。我使用 gdb 进行了调查,似乎 new_Message->Time 的地址等于 Check->Time 的地址。

它们都是不同类型的结构 我正在尝试解决这个问题,因为当解析器更改 new_Message->Time 的值时,它会操纵 Check 的内容->时间

请建议如何解决此问题。我已经失去(花费)了大约 10 个小时并计算解决这个问题,还有大量的头发。

索罕

编辑 添加结构定义

最佳答案

您正在使用堆栈分配对象的地址来初始化 new_Message->Time = &a[2][11];

关于c - 严重内存冲突: Variables clashing in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043044/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com