Do "pointers" exist in MATLAB? A question about the efficiency of using shorthands.

51 次查看(过去 30 天)
I'm not sure I'm using the right vocabulary, but here's what I mean.
Say I have data in this structure:
input.a.b.c.verylongname.thisistoolong.itmakesthecodehardtoread
This data will not be modified in the code I'm writing.
Say I create a shorthand:
muchsmaller = input.a.b.c.verylongname.thisistoolong.itmakesthecodehardtoread
and use that shorthand to access the data throughout the code.
Now, say that data is big. Will MATLAB actually create a copy of the data, using up memory, or will it use what I believe is called a "pointer" and simple point towards the same data regardless of the variable name I use?
Thank you!

采纳的回答

the cyclist
the cyclist 2023-2-4
编辑:the cyclist 2023-2-4
In general, MATLAB will not allocate new memory until it needs to, and will just use the same "array reference" to memory. See How MATLAB Allocates Memory for details.
That page does not specifically mention the case of copying from a structure to a numeric array, but I did a bit of testing, and confirmed that there is not an increase in memory use.
  1 个评论
James Tursa
James Tursa 2023-2-4
@Patrick Aoun The caveat here is that this doesn't behave like a pointer in C/C++. As long as you don't change the underlying data (as you state) things will behave as you expect. But if you change the underlying data, then a deep data copy will occur and your shorthand named variable will be disconnected from the structure variable from that point on.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by