简体   繁体   English

传递指向函数的指针而不复制它

[英]Passing pointers to function without copying it

How do I pass data around my program without copying it every time? 如何在程序周围传递数据而不每次都复制数据?

Specifically, when calling sim(ohlc) I want to just pass the pointer reference, I don't want to copy the data to the function. 具体来说,当调用sim(ohlc)我只想传递指针引用,就不想将数据复制到函数中。

This is the program I made, but I'm not sure this is the best way to do it (specially when it comes to speed and memory usage). 这是我制作的程序,但是我不确定这是执行该程序的最佳方法(特别是在速度和内存使用方面)。

I think I'm not passing the pointer to sim(ohlc) like I should, but if I try sim(&ohlc) I don't know how to change the sim function to accept that. 我想我没有像我应该的那样传递指向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);
}

It's C++, use a reference. 它是C ++,请使用参考。 It's safe, since you return a static object. 这是安全的,因为您返回了静态对象。

static ohlc ohlc_not_found;

ohlc &read_csv(string file_name) {
    // open file and read stuff
    if(error_while_opening)
    {
        return ohlc_not_found;
    }
    static ohlc loc_ohlc;
    loc_ohlc.timestamp.push_back(read_value);
    return loc_ohlc;
}

int sim(const ohlc& par_ohlc) {
    // do stuff
    return 1;
}

....
 ohlc& var_ohlc = read_csv(input_file);
 if(var_ohlc == ohlc_not_found)
 {
      // error handling
      return;
 }
 results = sim(var_ohlc);

If you want to modify par_ohlc in sim, do not make it const. 如果要在sim中修改par_ohlc ,请不要将其par_ohlc const。

and it's not recommended to use ohlc for both class and variable name :( 并且不建议将ohlc用于类名和变量名:(

In line: 排队:

results = sim(ohlc);

you are passing ohlc pointer to sim function, no deep data copy is done, only 32bit pointer value is copied. 您正在将ohlc指针传递给sim函数,没有完成深层数据复制,仅复制了32位指针值。

This pushes the address (32 bit value) onto the stack. 这会将地址(32位值)压入堆栈。

results = sim(ohlc);

Like: 喜欢:

  ; ...
  push eax ; addr of struct/class/whatever
  call function ; jump to function

  ; ...

function:
  push ebp
  mov ebp, esp
  mov eax, [ebp+8] ; ebp+8 is the 32 bit value you pushed before onto the stack
                   ; -> your pointer

Take a look at this and maybe that too. 看看这个 ,也许太。

Version 2 版本2

  ; ...
  push eax ; addr of struct/class/whatever
  jmp function ; jump to function
autolbl001:

  ; ...

function:
  push ebp
  mov ebp, esp
  mov eax, [ebp+8] ; ebp+8 is the 32 bit value you pushed before onto the stack
  ; ...
  jmp autolbl001

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM