Stack, Frame, Global Pointer and ELF files - stack

How do I determine where the stack, global, and frame pointers are in my ELF file?

Stack is setup when process or thread run. function or routine body contain codes that how to be called or call other subroutines, they indicate how much stack space will be used.
Frame pointer is related to stack frame which are dynamic updated.
Global variables are in data section in ELF file.
Hope this can help you.

Related

In iOS block how to modify variables outside without using __block

I read that if block wants to modify a variable outside, this variable has to be described by using keyword __block since it has to be copied to heap with block itself.
But what if I do not want to use __block and still get to modify the variable, is there a way?
I did some thinking that maybe I can set that variable to global or static instead of an auto one so that it will be stored in static memory area.
Or can I set it to pointer and point to some content which is allocated on heap? I tried this case which didn't work, I would like to know why.
Essentially, in order to avoid losing the variable, block would copy the local variable onto heap if you would like to access and modify the variable. But if you make the variable global or static, which would be stored on global region and you don't need to worry losing it on stack, block wouldn't copy it but is able to modify it directly.
Please correct me if any mistake and I will mark myself answer if there isn't any better one.

CCMenuItemLabel – "The block will be 'copied'."

Simple question regarding the statement “The block will be ‘copied’.” I am not quite comfortable with my understanding and use of blocks in objective-c, what does ‘copied’ mean? If someone can explain or point me to a resource which can, I would appreciate it. Thank you
*This method is from the Cocos2d documentation.
+ (id) itemWithLabel:(CCNode< CCLabelProtocol, CCRGBAProtocol > *) label
block:(id sender) block
creates a CCMenuItemLabel with a Label and a block to execute. The block will be “copied”.
Blocks are something like structure which contains a set of informations like the pointer to the function.
By default when you create a block, it s created on the Stack, you must copy it to the Heap to be able to use it outside the stack Call.
In this example this method will return an item object and will copy the block parameter(Probably still on the stack) to the Heap to be able to use it if needed.

Alternative for ReAllocMem that zeros the new allocation

I need to ReAllocMem a block, but the extension should be zero'ed.
For this reason ReAllocMem is no good:
From the helpfile:
ReallocMem reallocates a memory block.
[...]
The content of the newly allocated memory is not set to zero.
I've looked at ReAllocMemory, but the help does not state anything about zeroing the new allocation, it only states:
Note: ReallocMemory is the version of ReallocMem compatible with C++.
Is there an alternative that does zero the newly allocated memory?
The simple answer is no. The only raw memory reallocator is ReallocMem. There is nothing else. The design of a re-allocator is always to preserve the contents that were there before. You will have to write your own routine.
David is right but using WinAp it's possible: you can use Global Alloc and GlobalReAlloc using the GMEM_ZEROINIT flag.

Why is block designed to be allocated on the stack unless copied?

Why has Apple designed block to be allocated on the stack unless copied? What's the benefit for such behavior? Why not just make it behave like a regular NSObject - alloc-init it and it goes on the heap automatically?
The reason why blocks are placed on the stack by default is speed. In
the common case where the lifetime of the block is less than that of
the stack function that contains it, this is a very good optimisation.
http://www.cocoawithlove.com/2009/10/how-blocks-are-implemented-and.html

Defining Objective-C blocks as properties - best practice

I've recently come across an Apple document that shows the following property declaration for a block:
#interface XYZObject : NSObject
#property (copy) void (^blockProperty)(void);
#end
Also, this article states:
Note: You should specify copy as the property attribute, because a block needs to be copied to keep track of its captured state outside of the original scope. This isn’t something you need to worry about when using Automatic Reference Counting, as it will happen automatically, but it’s best practice for the property attribute to show the resultant behavior. For more information, see Blocks Programming Topics.
I also read the suggested Blocks Programming Topics but haven't found anything relevant there.
I'm still curious as to why defining a block property as "copy" is best practice. If you have a good answer, please try to distinguish between ARC and MRC differences if there are any.
Thank you
By default blocks are created on the stack. Meaning they only exist in the scope they have been created in.
In case you want to access them later they have to be copied to the heap by sending a copy message to the block object. ARC will do this for you as soon as it detects a block needs to be accessed outside the scope its created in. As a best practise you declare any block property as copy because that's the way it should be under automatic memory management.
Read Stack and Heap Objects in Objective-C by Mike Ash for more info on stack vs. heap.
Blocks are, by default, allocated on the stack. This is an optimization, since stack allocation is much cheaper than heap allocation. Stack allocation means that, by default again, a block will cease to exist when the scope in which it is declared exits. So a block property with retain semantics will result in a dangling pointer to a block that doesn't exist anymore.
To move a block from the stack to the heap (and thus give it normal Objective-C memory management semantics and an extended lifetime), you must copy the block via [theBlock copy], Block_copy(theBlock), etc. Once on the heap, the block's lifetime can be managed as needed by retaining/releasing it. (Yes, this applies in ARC too, you just don't have to call -retain/-release yourself.)
So you want to declare block properties with copy semantics so the block is copied when the property is set, avoiding a dangling pointer to a stack-based block.
The "best practices" you refer to simply say, "Seeing as ARC is going to magically copy your block no matter what you write here, it's best you explicitly write 'copy' so as not to confuse future generations looking at your code."
Explanation follows:
Typically, you shouldn’t need to copy (or retain) a block. You only need to make a copy when you expect the block to be used after destruction of the scope within which it was declared. Copying moves a block to the heap.
–Blocks Programming Topics: Using Blocks, Copying Blocks
Clearly, assigning a block to a property means it could be used after the scope it was declared in has been destroyed. Thus, according to Blocks Programming Topics that block should be copied to the heap with Block_copy.
But ARC takes care of this for you:
Blocks “just work” when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more.
–Transitioning to ARC
Note that this isn't about the retain semantics the block. There's simply no way for the block's context to exist without being moved off the (soon-to-be-popped) stack and on to the heap. So regardless of what attributes you qualify your #property with, ARC is still going to copy the block.

Resources