init variable in c++ -
i encountered strange problem, @ times, @ no. when read data file insert map, data read file attached struct type traderecord , insert map. when init object order_tmp don't assign value 0 have field in traderecord don't have in file assign junk value c++ compiler.
//struct traderecord struct traderecord { int order; // order ticket int login; // owner's login char symbol[12]; // security int digits; // security precision int cmd; // trade command int volume; // volume //--- __time32_t open_time; // open time int state; // reserved double open_price; // open price double sl,tp; // stop loss & take profit __time32_t close_time; // close time int gw_volume; // gateway order volume __time32_t expiration; // pending order's expiration time char reason; // trade reason char conv_reserv[3]; // reserved fields double conv_rates[2]; // convertation rates profit currency group deposit currency // (first element-for open time, second element-for close time) double commission; // commission double commission_agent; // agent commission double storage; // order swaps double close_price; // close price double profit; // profit double taxes; // taxes int magic; // special value used client experts char comment[32]; // comment int gw_order; // gateway order ticket int activation; // used mt manager short gw_open_price; // gateway order price deviation (pips) order open price short gw_close_price; // gateway order price deviation (pips) order close price double margin_rate; // margin convertation rate (rate of convertation margin currency deposit one) __time32_t timestamp; // timestamp int api_data[4]; // api usage traderecord *__ptr32 next; // internal data }; //code read data file while (file.good()) { std::getline(file, line_str); boost::split(fields, line_str, boost::is_any_of(k_delimiter)); // line don't enough data if (fields.size() < k_line_fields) { log(debug) << m_log_tag << "ignore line " << line << ", not enough data: " << line_str; line++; continue; } log(debug) << m_log_tag << "data line " << line << ": " << line_str; traderecord order_tmp; order_tmp.login = atoi(fields[0].c_str()); order_tmp.order = atoi(fields[1].c_str()); strncpy_s(order_tmp.symbol, _countof(order_tmp.symbol), fields[2].c_str(), _truncate); order_tmp.volume = atoi(fields[3].c_str()); order_tmp.cmd = atoi(fields[4].c_str()); order_tmp.open_price = atof(fields[5].c_str()); order_tmp.margin_rate = atof(fields[6].c_str()); order_tmp.open_time = atoi(fields[7].c_str()); list_open_order.insert(std::make_pair(order_tmp.order, order_tmp)); log(debug) << std::fixed << "for test, read open order: " << order_tmp.order << ", swap=" << order_tmp.storage; line++; }
like can see, in traderecord has many fields in file not enough, c++ auto assign value these(-92559631349317830000000000000000000000000000000000000000000000.000000 this)
but not understand why problem when not because normal
you should not use variable without giving default value. in c++ (unlike other programming languages java) not auto-initialize variables. in case, integer in stack value of whatever random data there.
Comments
Post a Comment