FROM : Bill Bumgarner
DATE : Thu Jul 27 00:25:18 2006
On Jul 26, 2006, at 2:16 PM, D.K. Johnston wrote:
> I understand that if I want the scanned characters to be placed in:
>
> NSString *string;
>
> I need to send "&string" to the method, which then presumably does
> something like this:
>
> *string = [internalWorkingString copy];
>
> But if the method got "string" instead of "&string", couldn't it
> just do this:
>
> string = [internalWorkingString copy];
>
> with the same result?
Much easier in code:
void foo(NSString *a, NSString **b) {
NSLog(@"1. &a 0x%x", &a);
NSLog(@"1. a 0x%x", a);
NSLog(@"1. b 0x%x", b);
NSLog(@"1. *b 0x%x", *b);
a = [@"foo" copy];
*b = [@"bar" copy];
NSLog(@"2. a 0x%x", a);
NSLog(@"2. b 0x%x", b);
NSLog(@"2. *b 0x%x", *b);
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *a = nil;
NSString *b = nil;
NSLog(@"0. &a 0x%x", &a);
NSLog(@"0. a 0x%x", a);
NSLog(@"0. b 0x%x", b);
NSLog(@"0. &b 0x%x", &b);
foo(a, &b);
NSLog(@"3. a 0x%x", a);
NSLog(@"3. b 0x%x", b);
NSLog(@"3. &b 0x%x", &b);
[pool release];
return 0;
}
Which outputs:
2006-07-26 15:22:45.057 foobar[676] 0. &a 0xbffffa18
2006-07-26 15:22:45.057 foobar[676] 0. a 0x0
2006-07-26 15:22:45.057 foobar[676] 0. b 0x0
2006-07-26 15:22:45.057 foobar[676] 0. &b 0xbffffa14
2006-07-26 15:22:45.057 foobar[676] 1. &a 0xbffffa00
2006-07-26 15:22:45.057 foobar[676] 1. a 0x0
2006-07-26 15:22:45.057 foobar[676] 1. b 0xbffffa14
2006-07-26 15:22:45.057 foobar[676] 1. *b 0x0
2006-07-26 15:22:45.058 foobar[676] 2. a 0x2404c
2006-07-26 15:22:45.058 foobar[676] 2. b 0xbffffa14
2006-07-26 15:22:45.058 foobar[676] 2. *b 0x2405c
2006-07-26 15:22:45.058 foobar[676] 3. a 0x0
2006-07-26 15:22:45.058 foobar[676] 3. b 0x2405c
2006-07-26 15:22:45.058 foobar[676] 3. &b 0xbffffa14
Note that the address of a is different in foo() than in main().
Variables passed to functions are copied -- the bytes are copied, not
it invokes -copy to copy the contents of the variable -- as they are
passed to a function. 'b' works because you are a passing a pointer
to a pointer to a string... the pointer to the pointer is copied and,
thus, foo() can then set the pointer's value even though it is
actually a copy of the pointer to the pointer that was passed in.
Perfectly clear, right? :-)
DATE : Thu Jul 27 00:25:18 2006
On Jul 26, 2006, at 2:16 PM, D.K. Johnston wrote:
> I understand that if I want the scanned characters to be placed in:
>
> NSString *string;
>
> I need to send "&string" to the method, which then presumably does
> something like this:
>
> *string = [internalWorkingString copy];
>
> But if the method got "string" instead of "&string", couldn't it
> just do this:
>
> string = [internalWorkingString copy];
>
> with the same result?
Much easier in code:
void foo(NSString *a, NSString **b) {
NSLog(@"1. &a 0x%x", &a);
NSLog(@"1. a 0x%x", a);
NSLog(@"1. b 0x%x", b);
NSLog(@"1. *b 0x%x", *b);
a = [@"foo" copy];
*b = [@"bar" copy];
NSLog(@"2. a 0x%x", a);
NSLog(@"2. b 0x%x", b);
NSLog(@"2. *b 0x%x", *b);
}
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *a = nil;
NSString *b = nil;
NSLog(@"0. &a 0x%x", &a);
NSLog(@"0. a 0x%x", a);
NSLog(@"0. b 0x%x", b);
NSLog(@"0. &b 0x%x", &b);
foo(a, &b);
NSLog(@"3. a 0x%x", a);
NSLog(@"3. b 0x%x", b);
NSLog(@"3. &b 0x%x", &b);
[pool release];
return 0;
}
Which outputs:
2006-07-26 15:22:45.057 foobar[676] 0. &a 0xbffffa18
2006-07-26 15:22:45.057 foobar[676] 0. a 0x0
2006-07-26 15:22:45.057 foobar[676] 0. b 0x0
2006-07-26 15:22:45.057 foobar[676] 0. &b 0xbffffa14
2006-07-26 15:22:45.057 foobar[676] 1. &a 0xbffffa00
2006-07-26 15:22:45.057 foobar[676] 1. a 0x0
2006-07-26 15:22:45.057 foobar[676] 1. b 0xbffffa14
2006-07-26 15:22:45.057 foobar[676] 1. *b 0x0
2006-07-26 15:22:45.058 foobar[676] 2. a 0x2404c
2006-07-26 15:22:45.058 foobar[676] 2. b 0xbffffa14
2006-07-26 15:22:45.058 foobar[676] 2. *b 0x2405c
2006-07-26 15:22:45.058 foobar[676] 3. a 0x0
2006-07-26 15:22:45.058 foobar[676] 3. b 0x2405c
2006-07-26 15:22:45.058 foobar[676] 3. &b 0xbffffa14
Note that the address of a is different in foo() than in main().
Variables passed to functions are copied -- the bytes are copied, not
it invokes -copy to copy the contents of the variable -- as they are
passed to a function. 'b' works because you are a passing a pointer
to a pointer to a string... the pointer to the pointer is copied and,
thus, foo() can then set the pointer's value even though it is
actually a copy of the pointer to the pointer that was passed in.
Perfectly clear, right? :-)
| Related mails | Author | Date |
|---|---|---|
| D.K. Johnston | Jul 26, 23:16 | |
| Bill Bumgarner | Jul 27, 00:25 |






Cocoa mail archive

