How do I create a stack-based character array that can be captured (const copied) by a block closure? - objective-c-blocks

I'm trying to figure out a clean way to create a stack-based C string (i.e. char[]) that can be captured (by const copy) by a block closure. The basic idea is like this:
char myString[16] = {0};
// ... put something into myString.
dispatch_block_t block = ^(){
const size_t len = strlen(myString);
if (len)
NSLog(#"Not zero length");
};
But doing that causes this compiler complaint:
error: cannot refer to declaration with an array type inside block
It occurs to me that I could put the char array into a struct, but that seems kinda ugly. Is there a better way?

The problem with stack allocation is that the stack frame disapears once you leave the function but the block code still remains and may refer to variables in that stack frame. Blocks handle this by allocating and copying all values refered by it to the heap including variables declared with __block. In theory they should be able to copy statically sized arrays but for some reason it's not supported. Mind you there could very well be technical issues that I'm not aware of related to arrays.
So I the simplest solution I see is to use malloc/free:
char *myString = calloc(16,1);
dispatch_block_t block = ^(){
const size_t len = strlen(myString);
if (len)
NSLog(#"Not zero length");
free(myString);
};
So long as you don't need to re-use the block multiple times you are OK. If you need to re-use the block then wrap myString in a NSData object that you can refer to in your block and get rid of the free.
char *myString = calloc(16,1);
NSData *myStringData = [NSData dataWithBytesNoCopy:myString length:16 freeWhenDone:YES];
dispatch_block_t block = ^(){
const char *myString = myStringData.bytes;
const size_t len = strlen(myString);
if (len)
NSLog(#"Not zero length");
};

Having been a week at this point, I'm starting to feel confident that there's not some magic syntax that I'm simply missing. For the benefit of future visitors, this is the best way I've found so far to achieve what I'm asking about here:
typedef struct { char string[32]; } StackString;
StackString foo = {0};
strcpy(foo.string, "foo");
dispatch_block_t block = ^(){
StackString blockFoo = foo;
NSLog(#"Block String: %s", blockFoo.string);
};
for (int i = 0; i < 5; ++i)
{
sprintf(foo.string, "i = %d", i);
NSLog(#"Loop string: %s", foo.string);
block();
}
The output of which is:
2013-03-31 11:30:52.778 TestClosure[98968:303] Loop string: i = 0
2013-03-31 11:30:52.780 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.780 TestClosure[98968:303] Loop string: i = 1
2013-03-31 11:30:52.780 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.780 TestClosure[98968:303] Loop string: i = 2
2013-03-31 11:30:52.781 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.781 TestClosure[98968:303] Loop string: i = 3
2013-03-31 11:30:52.782 TestClosure[98968:303] Block String: foo
2013-03-31 11:30:52.782 TestClosure[98968:303] Loop string: i = 4
2013-03-31 11:30:52.782 TestClosure[98968:303] Block String: foo
This convinces me that wrapped in a struct, the fixed size array is const-copied into the block closure just fine.

Related

How to replace a char in an an char array? Xcode

i got the following char array in Objective-C (Xcode):
char *incomeMessage;
NSString *str = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
incomeMessage = [str UTF8String];
NSLog(#"%c", incomeMessage[0]);
NSLog(#"%c", incomeMessage[1]);
NSLog(#"%c", incomeMessage[2]);
NSLog(#"%c", incomeMessage[3]);
NSLog(#"%c", incomeMessage[4]);
NSLog(#"%c", incomeMessage[5]);
For example I get some results like this in console:
"3
2
6
1
8
4"
Now i want to replace the char in incomeMessage[2] by 4:
incomeMessage[2] = '4';
But then it gives me the error:
EXC_BAD_ACCESS
Do you have an idea, how to solve the problem?
According to the reference documentation, UTF8String returns a read-only (const char*) reference to the string data.
The reference material goes on to note:
This C string is a pointer to a structure inside the string object,
which may have a lifetime shorter than the string object and will
certainly not have a longer lifetime. Therefore, you should copy the C
string if it needs to be stored outside of the memory context in which
you use this property.
So I'd suggest following their advice and creating a copy of the array and then performing your modifications against that.
For example: http://ideone.com/mhjwZW
You might have better luck with something like:
NSString* str = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
char* incomeMessage = malloc([str lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
strcpy(incomeMessage, [str UTF8String]);
//now you can change things
incomeMessage[2] = '4';
//do this when you're done
free(incomeMessage);
Although, is there any particular reason why you want to use a C-string/character array as opposed to an NSMutableString? I think you might find replaceCharactersInRange:withString: a better approach generally. See also: stringByReplacingCharactersInRange:withString:.
i got the following char array in Objective-C (Xcode)
You don't, you know. All you have is a pointer. You have not set aside any actual memory; there is no array there.
incomeMessage = [str UTF8String];
All you've done in that line is repoint the pointer incomeMessage at your string's UTF8String. A string's UTF8String is immutable. Note this passage in the docs:
you should copy the C string if it needs to be stored outside of the memory context in which you use this property.
So basically, if you want to write into an array of char, your first task should be to make an array of char.

NSData pointer vs reference

I'm dealing with the garmin GDL90 protocol which sends across various types of messages in binary to my IOS device. I'm going through and trying to process all these messages but have been running into an issue. Specifically the messages are byte packed so that if you ever see an occurrence of
0x7d 0x5e or 0x7d 0x5d you have to convert them to 0x7d or 0x7e
I've set my code up so that I detect the message type I'm parsing and then call a function:
- (void) parseMessage:(NSMutableData *)message
to do my data parsing. My individual message parsing functions call the parent function [super parseMessage:message]; which handles both the parsing of common elements as well as dealing with my byte-stuffing. Each of these function calls takes an NSData * so shouldn't a modification made in my super function return back out the same data?
My top level class gets a parse message call and the NSMutableData pointer's address is: 0x170048f10
Once I step into the parent's parseData call my address is still 0x170048f10
After I make modifications to the data I'm now pointing at the memory address 0x17805e840
Once I return from this function, however, I'm back pointing at 0x170048f10 again with the wrong data.
Should I be using pass by reference or something? Any suggestions?
I have two variations of my function - unstuff1 throws an error and unstuff2 doesn't work.
- (NSMutableData *)unstuff1:(NSMutableData *)mutableData {
int dataLength = [mutableData length];
char *bytes = [mutableData bytes];
// Scan bytes ignoring 1st and last byte because they will be 7e's
for (int i = dataLength - 1; i > 0; i--) {
bytes[i + 1] ^= 0x20;
if (i + 1 == dataLength) {
NSLog(#"Terminal character padding detected on character %d with length %d", i, dataLength);
} else {
/* Replace 2 bytes with a single byte should remove the flag when you do this */
[mutableData replaceBytesInRange:NSMakeRange(i, 2) withBytes:&bytes[i + 1] length:1];
dataLength--;
}
}
return mutableData;
}
- (NSMutableData *)unstuff2:(NSMutableData *)data {
NSMutableData *mutableData = [[NSMutableData alloc] initWithData:data];
int dataLength = [mutableData length];
char *bytes = [mutableData bytes];
// Scan bytes ignoring 1st and last byte because they will be 7e's
for (int i = dataLength - 1; i > 0; i--) {
bytes[i + 1] ^= 0x20;
if (i + 1 == dataLength) {
NSLog(#"Terminal character padding detected on character %d with length %d", i, dataLength);
} else {
/* Replace 2 bytes with a single byte should remove the flag when you do this */
[mutableData replaceBytesInRange:NSMakeRange(i, 2) withBytes:&bytes[i + 1] length:1];
dataLength--;
}
}
return mutableData;
}
In unstuff2 obviously i'm making a new MutableData so I guess that accounts for the memory address change (that is the function i was using that gave me the error specified).
unstuff1 throws the following exception:
-[_NSInlineData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x178250d40
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSInlineData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance
Unlike languages like C++ and C# (just to name two), Objective C has no concept of "pass by reference". However, passing a copy of a pointer to your NSMutableData is functionally equivalent to passing the object by reference. That is, if you pass in an NSMutableData (or NSMutableAnything for that matter) to a function and that function modifies it, the calling function will see the changes reflected in the object that it passed in.
Ok looks like I tracked down the problem. I realized the compiler was sending out warnings:
incompatible pointer types initializing 'NSMutableData *' with an expression of type 'NSData *'
It turns out I had some code
NSMutableData *message = [data subdataWithRange:NSMakeRange(5, len - 5)];
Which i needed to convert into:
NSMutableData *message = [NSMutableData dataWithData:[data subdataWithRange:NSMakeRange(5, len - 5)]];
And then things all work out. Moral of the story (read your warnings!!!!)

Objective-C blocks

Trying to understand how blocks working in objective-c. Got next question while reading apple's docs (link)
Here is an example how we should no use blocks:
void dontDoThis() {
void (^blockArray[3])(void); // an array of 3 block references
for (int i = 0; i < 3; ++i) {
blockArray[i] = ^{ printf("hello, %d\n", i); };
// WRONG: The block literal scope is the "for" loop.
}
}
But how we could get 3 different blocks that will print "hello, 0", "hello, 1" and "hello, 2"? I tried many different ways but every time I got "hello, 2" three times.
A block starts out life on the stack and, thus, a block's lifespan is only as long as the scope it is declared in.
The body of a for() loop -- the body of the loop in the {}s -- is a scope in and of itself. Thus, your code is putting a reference to something on the stack [the block] into a variable in the surrounding scope [the language array].
You need to copy the block to the heap to have it survive:
void dontDoThis() {
void (^blockArray[3])(void); // an array of 3 block references
for (int i = 0; i < 3; ++i) {
blockArray[i] = [^{ printf("hello, %d\n", i); } copy];
}
}
If not using ARC, you would also need to -release the copied blocks at some point.
You might find this weblog post handy (I wrote it shortly after Blocks were made public). This one goes into a few tips, tricks, and gotchas.
Wait -- yeah -- you're correct. There is magic going on in the ARC compiler that is causing the blocks to seemingly be on the heap magically. However, I can't find anything in the LLVM documentation that explicitly documents this behavior. If you turn off ARC, you'll see the output be something like 2,2,2 instead of 0,1,2.
This is somewhat new behavior. I wouldn't rely on this behavior until someone can find the explicit note in the compiler that defines exactly how this is supported.
#autoreleasepool {
void (^blockArray[3])(void); // an array of 3 block references
for (int i = 0; i < 3; ++i) {
void (^block)(void) = ^{ printf("hello, %d\n", i); };
NSLog(#"%p", block);
blockArray[i] = block;
NSLog(#"%p", blockArray[i]);
}
for (int i = 0; i < 3; ++i) blockArray[i]();
}
Outputs:
2012-12-24 16:15:36.752 jkdfjkfdjkdfjk[70708:303] 0x7fff5fbff838
2012-12-24 16:15:36.755 jkdfjkfdjkdfjk[70708:303] 0x100108160
2012-12-24 16:15:36.758 jkdfjkfdjkdfjk[70708:303] 0x7fff5fbff838
2012-12-24 16:15:36.759 jkdfjkfdjkdfjk[70708:303] 0x100108000
2012-12-24 16:15:36.760 jkdfjkfdjkdfjk[70708:303] 0x7fff5fbff838
2012-12-24 16:15:36.760 jkdfjkfdjkdfjk[70708:303] 0x100102e70
hello, 0
hello, 1
hello, 2
Thus, the block is created on the stack and copied to the heap automatically on the assignment outside of the scope of the for() loop.
A similar test also reveals that the block will be copied when passed as an argument to NSArray's addObject:.
If you really wanted to get this to work you could use an NSMutableArray instead of a C array.
NSMutableArray *blocks = [NSMutableArray array];
for (int i = 0; i <= 3; i++) {
blocks[i] = ^{ printf("hello %d\n", i); };
}
By adding them to an NSMutableArray they will be copied off of the stack and onto the heap allowing them to outlive the scope of the for loop.
As #bbum points out the above does not work, I took the idea that blocks just work with ARC too far.
You would need to actively copy the blocks for them to work... so the following should work
NSMutableArray *blocks = [NSMutableArray array];
for (int i = 0; i <= 3; i++) {
blocks[i] = [^{ printf("hello %d\n", i); } copy];
}

How to Convert NSMutableArray objects to const char in xcode

To develop a calculator in xcode I am using a c class for converting infix to postfix expression and its evaluation. But I have an NSString in my View controller class and I need to pass this NSString to a C class where the conversion and evaluation happens. How can I do this?
I think you need to convert NSString to cString. It can be done by
[str UTF8String]
Assuming you have an NSMutableArray containing NSString objects, and you want to convert this to a C array containing C strings, you need to allocate memory for a C array of char * of suitable size (e.g., count of the NSMutableArray + maybe 1 extra if you want a NULL terminator for the array, otherwise you need to pass along the array's element count everywhere). Then for each element of the NSMutableArray, populate the corresponding index in the C array with the C string returned by UTF8String of the NSString object.
Note that the C strings returned by UTF8String are “autoreleased”, so if you need to keep them around for longer than the current autorelease context, you need to duplicate them (e.g., strdup) and then free them (and the C array, which you need to free in any case) after you're done with them.
For example:
#include <Foundation/Foundation.h>
#include <stdio.h>
#include <string.h>
void printCStringsInArray(const char **arr) {
int i = 0;
while (*arr) { printf("%d: %s\n", i++, *arr++); }
}
int main () {
const char **carr;
#autoreleasepool {
NSMutableArray *arr;
const char **p;
arr = [NSMutableArray arrayWithObjects:#"11", #"+", #"12", nil];
p = carr = malloc(sizeof(*carr) * ([arr count] + 1));
for (NSString *s in arr) {
*p++ = strdup([s UTF8String]);
}
*p = NULL; // mark the end of carr
}
printCStringsInArray(carr);
{ // free the C strings and array
const char **p = carr;
while (*p) { free(*p++); };
free(carr);
}
return 0;
}
This prints:
0: 11
1: +
2: 12
edit: Of course if you just want to call some C function taking individual strings as const char * from your otherwise Objective-C code, you don't need to do anything this complicated, just convert each string on the fly with UTF8String and use the NSMutableArray for iteration, etc. But then one might wonder what it is that you can do with C strings that you couldn't do directly with NSStrings.
It's easy because you can use NSString's UTF8String method, but you have to handle memory allocation: allocate a block to contain the string.Choose if to wrap it into a NSData so that it will be released automatically when out of the pool block, or if to free it manually:
#autoreleasepool
{
NSArray* strings= #[ #"Hello", #"Hey" ]; // Your strings
// This could be also a NSMutableArray if you need it
NSMutableArray* cstrings=[NSMutableArray new]; // C strings
for(NSString* string in strings) // Iterate over all the strings
{
const char* temp=[string UTF8String]; // Get C string
const unsigned long length=strlen(temp); // Allocate memory for it
char* string=(char*)malloc((length+1)*sizeof(char));
strcpy(string,temp); // Copy it
// Store it inside NSData
[cstrings addObject: [NSData dataWithBytesNoCopy: string length: (length+1)*sizeof(char)]];
}
}

how to extract data from cocoa iPhone sax xml parsing routine

I'm trying to read in and parse an xml document in an iPhone app. I begin parsing and then use the override method:
static void startElementSAX(void *ctx, const xmlChar *localname, const xmlChar *prefix, const xmlChar *URI,
int nb_namespaces, const xmlChar **namespaces, int nb_attributes, int nb_defaulted, const xmlChar **attributes)
I then try to convert the attributes to a string with:
NSString *str1 = [[NSString alloc] initWithCString:attributes encoding:NSUTF8StringEncoding];
Why does the attributes parameter have two ** in front of it. And why when trying to extract the data and convert it to a string with the above code do I get the warning:
passing argument 1 of 'initWithCString:encoding:' from incompatible pointer type.
The documentation for libxml's start element callback states that the pointer is to an array that hold 5 values for each attribute (the number of attributes is returned in nb_attributes). This means that every 5th value in the array is a new attribute item.
The five items for each attribute are:
localname (the name of the attribute)
prefix (the namespace of the attribute)
URI
[start of] value (a pointer to the start
of the xmlChar string for the value)
end [of value] (a pointer to the end of the
xmlChar string for the value)
So you need to step through the array, get each value out of the items for the first attribute, then use the start value pointer to get the xmlChar string that is length = end - start. Then start over with the next attribute till you read in nb_attributes worth.
If that makes your head ache then I strongly suggest you switch to Apple's NSXMLParser (link may require login, or use this link NSXMLParser). In which case you would get the attributes as an NSDictionary. To get all the attributes out of it you could do the following:
for (NSString *attributeName in [attributeDict allKeys]) {
NSString *attributeValue = [attributeDict objectForKey:attributeName];
// do something here with attributeName and attributeValue
}
If you have access to the iPhone developer site then look at the example SeismicXML.
The sample is great except for two things:
you need to bump 'i' by 5 after each loop since there are 5 items for each attribute.
doing strlen() on both begin and end is expensive; it's easier to simply subtract begin from end
for (int i = 0; i < nb_attributes*5; i += 5)
{
const char *attr = (const char *)attributes[i];
const char *begin = (const char *)attributes[i + 3];
const char *end = (const char *)attributes[i + 4];
int vlen = end - begin;
char val[vlen + 1];
strncpy(val, begin, vlen);
val[vlen] = '\0';
NSLog(#"attribute %s = '%s'", attr, val);
}
The accepted answers explanation is correct, but it's helpful to view some example code too. Here is just one way to extract the value from the attributes, at least it works when I tested it. I'm far from being a C guru though.
for (int i = 0; i < nb_attributes; i += 5) {
const char *attr = (const char *)attributes[i];
const char *begin = (const char *)attributes[i + 3];
const char *end = (const char *)attributes[i + 4];
int vlen = strlen(begin) - strlen(end);
char val[vlen + 1];
strncpy(val, begin, vlen);
val[vlen] = '\0';
NSLog(#"attribute %s: %d = %s", attr, i, val);
}
NSXMLParser is nice, but from what I can tell, it downloads the entire XML before processing. Using libxml it can read in chunks at a time. It allows greater flexibility, but higher learning curve.
The '**' notation means "pointer to a pointer." In C/C++, a "string" is represented by an array of characters. An array is actually just a pointer under the covers, so a string in C/C++ can actually be declared as either "char[]" or "char*". The [] notation compiles down to a pointer to an array.
A common example of this is the typical "main" function in C/C++:
int main(int argc, char **argv)
Which is equivalent to:
int main(int argc, char *argv[])
argv is an array of char* "strings" (the command-line arguments to the program).
I can't provide an example at the moment, but it looks like you need to iterate over attributes to access the individual strings. For example, attributes[0] would be the first attribute string (an xmlChar*). You should be able to convert each individual attribute to an NSString.
const xmlChar **namespaces is an array of CStrings (int nb_namespaces tells you how many). If you want each namespace as an NSString, you could do something like the following:
NSMutableArray *namespaces = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < nb_namespaces; i++) {
NSString *namespace = [[NSString alloc] initWithCString:attributes[i] encoding:NSUTF8StringEncoding];
[namespaces addObject:namespace];
}
The initWithCString method is expecting xmlChar *, which is a pointer to an xmlChar (the first char in a CString).
xmlChar ** means pointer to a pointer to an xmlChar (the first char in the first CString).