【发布时间】:2014-01-23 12:52:53
【问题描述】:
如何在程序中传递数据而不是每次都复制?
具体来说,在调用sim(ohlc)时我只想传递指针引用,不想将数据复制到函数中。
这是我制作的程序,但我不确定这是最好的方法(特别是在速度和内存使用方面)。
我认为我没有像我应该的那样将指针传递给sim(ohlc),但是如果我尝试sim(&ohlc),我不知道如何更改sim 函数来接受它。
struct ohlcS {
vector<unsigned int> timestamp;
vector<float> open;
vector<float> high;
vector<float> low;
vector<float> close;
vector<float> volume;
} ;
ohlcS *read_csv(string file_name) {
// open file and read stuff
if (read_error)
return NULL;
static ohlcS ohlc;
ohlc.timestamp.push_back(read_value);
return &ohlc;
}
int sim(ohlcS* ohlc) {
// do stuff
return 1;
}
main() {
ohlcS *ohlc = read_csv(input_file);
results = sim(ohlc);
}
【问题讨论】:
-
您传递给
sim()-函数的正是一个指针。 -
当您将指针传递给
sim()时,您不会复制数据。只是指针。 -
只是一个建议,不要使用类名作为变量名 ohlc *ohlc 很混乱。
-
@VictorPolevoy 那么,这是正确的做法吗?
-
@EricFortin 好的,我会改变它。