开发者

Might a std::vector created in managed C++/cli on the (managed) stack change memory locations?

If in C++/cli I write

ref class test {
    void method() {<
       std::vector<float> my_stack_vector;
       native_function_开发者_运维问答call( & my_stack_vector );
    }    
};

Can the location of my_stack_vector change after its creation? I know that if it lived in the the underlying native stack then it wouldn't change, but with the extra layer of abstraction .Net creates I don't know whether it is subject to moving in memory.

More importantly, how can I verify that for myself, e.g. what would I look for using ILDASM or in the native dissassembly after it is compiled into native code at run time or somewhere else to be able to answer such questions on my own in the future?


No, you're fine with this code. The garbage collector only moves managed (ref class) objects allocated on the GC heap. It doesn't mess with the stack or the JIT compiled code.

You don't have to worry about inducing bugs like this, the compiler always warns you about it. Try making the vector a member of the class for example. Now it would be a problem, the GC does move an object of type "test".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜