gcc - Serious Memory Clash: Variables clashing in C -
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 latest trade int qtytraded; float valuetraded; char* time; }; typedef struct asset asset; int main(int argc, char*argv[]) { message* newmessage; asset* check; //manipulation , initialization of check, 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); }
here there serious memory clash, in 2 variables of different scope. have investigated using gdb , seems address of new_message->time
equalling address of check->time
.
they both structures of different types trying resolve issue, because, when parser changes value of new_message->time
manipulates contents of check->time
please suggest how solve problem. have lost(spent) around 10 hours , counting resolve issue, , tons of hair.
soham
edit structure def added
you're using address of stack allocated object initialize new_message->time = &a[2][11];
Comments
Post a Comment