refreturn | Find functions that return a reference and cause allocations | Monitoring library
kandi X-RAY | refreturn Summary
kandi X-RAY | refreturn Summary
Find functions that return a reference and cause allocations. When a function allocates a value and returns a reference to it, the value has to escape to the heap. This is slower and puts pressure on the garbage collector.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- findAllocationsInFile walks the file at the given path .
- Run the files in the given directory .
- rootCommand returns the cobra command
- containsReference returns true if fieldList contains a StarExpr .
- sendFiles recursively sends the files to the given channel .
- Runs root command
refreturn Key Features
refreturn Examples and Code Snippets
Community Discussions
Trending Discussions on refreturn
QUESTION
I'm reading .
As for "return by reference", it says
There are two important restrictions on return by reference—both due to object lifetime: Object references shouldn’t be garbage collected while they’re still referenced, and they shouldn’t consume memory when they no longer have any references. To enforce these restrictions, you can only return the following from a reference-returning function:
• References to fields or array elements
• Other reference-returning properties or functions
• References that were passed in as parameters to the by-reference returning function
I did some experiments, and the results are certainly as the book says.
...ANSWER
Answered 2021-May-19 at 03:07A local variable has the same lifetime as the method, the memory location of the variable itself is on the stack.
So neither variable int a
or Test t
exist after the method returns.
But a[0]
and t.x
exist inside objects that are stored in heap memory. They will still be there when the method returns since they live outside the stack.
So how about an example. Why would use want to use a ref local or ref return? How about when defining a linked list;
QUESTION
In below code, compiler warns about returning reference to local when bar() method is called. I was expecting similar warning about foo() method as well.
...ANSWER
Answered 2019-Mar-24 at 14:32"Why does c++ compiler not warn about returning reference to local variable?"
Because compilers are not perfect and ultimately it is your responsibility to not write invalid code. The compiler is not obligated to warn on everything that is wrong (in fact, it's obligated to warn on very little, but most try to do better than the minimal requirement).
QUESTION
I'm trying to understand why/how does ref-return for the case of returning refs to members of a class. In other words, I want to understand the internal workings of the runtime that guarantee why ref-return of a instance member works, from the memory safety aspect of the CLR.
The specific feature I'm referring to is mentioned in the ref-return documentation which specifically states:
The return value cannot be a local variable in the method that returns it; it must have a scope that is outside the method that returns it. It can be an instance or static field of a class, or it can be an argument passed to the method. Attempting to return a local variable generates compiler error CS8168, "Cannot return local 'obj' by reference because it is not a ref local."
Here's a code snippet that cleanly compiles and runs and demonstrates returning an instance field as ref return:
...ANSWER
Answered 2017-Dec-20 at 21:12How does the .NET GC / runtime make sure that this would never result in an access violation or reading wild pointers after the SomeClass backing memory has been re-used for something else?
For the exact same reason that x
won't reference an object that has already been cleaned up by the GC. You know this because the GC isn't allowed to clean up an object if it's accessible through managed code. The object is reachable both through x
until you clear it, and also s
. s
is a reference to that object, and needs to be treated as such by the GC when determining if that object is allowed to be collected.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install refreturn
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page