Thank you ibex10.
Maybe I didn’t clarify my question. To clarify the reason I printed the address is to check if it was changing.
I have made this example code with four tests:
Test 1 string pointer testString is allocated and initiated for the first time and added to the array
result:
array[0] = "This is something"
comment: nothing special here string was added to the array.
Test 2 string pointer testString is allocated and initiated for the second time and added to the array
result:
array[0] = "This is something"
array[1] = "say what?"
comment: allocating and initiating the string added a new value to the array even though the string pointer address did not change and there was no implications on the first item in the array.
Test 3 string pointer testString is appended and added to the array
result:
array[0] = "This is something"
array[1] = "say what? this is how it is"
array[2] = "say what? this is how it is"
comment: appending the string pointer without allocating and initiating resulted in changing the values of both array[1] and array[2].
Test 4 string pointer testString is allocated and initiated for the third time using a two step process and added to the array
result:
array[0] = "This is something"
array[1] = "say what? this is how it is"
array[2] = "say what? this is how it is"
array[3] = "Back to square one"
comment: allocating and initiating the string pointer added a new value that did not have any effect on previous items even though the pointer address did not change.
having presented the above it is clear when we allocate and initiate the same string pointer for a second time something happens. what happens to the link between the first item in the array and the pointer it gained its value from?
I hope I was able to explain why I am a bit perplexed by this behavior as I would think the values in the array are associated with one pointer so I assumed (apparently wrongly so) that they should all have the value of the what ever the pointer value is at all locations in the array.
Code
[code]#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableString *testString;
NSMutableArray *testArray = [[NSMutableArray alloc] init];
// Test 1 string pointer testString is allocated and initiated for the first time
testString = [[NSMutableString alloc] initWithString:@"This is something"];
[testArray addObject:testString];
NSLog(@"***test 1***");
NSLog(@"testString pointer address: %p",&testString);
NSLog(@"%@", testString);
NSLog(@"testArray pointer: %p",&testArray);
NSLog(@"%@",testArray);
// Test 2 string pointer testString is allocated and initiated for the second time
testString = [[NSMutableString alloc] initWithString:@"say what?"];
[testArray addObject:testString];
NSLog(@"***test 2***");
NSLog(@"testString pointer address: %p",&testString);
NSLog(@"%@", testString);
NSLog(@"testArray pointer: %p",&testArray);
NSLog(@"%@",testArray);
// Test 3 string pointer testString is appended
[testString appendString:@" this is how it is"];
[testArray addObject:testString];
NSLog(@"***test 3***");
NSLog(@"testString pointer address: %p",&testString);
NSLog(@"%@", testString);
NSLog(@"testArray pointer: %p",&testArray);
NSLog(@"%@",testArray);
// Test 4 string pointer testString is allocated and initiated for the third time using a two step process
testString = [NSMutableString alloc];
testString = [testString initWithString:@"Back to square one"];
[testArray addObject:testString];
NSLog(@"***test 4***");
NSLog(@"testString pointer address: %p",&testString);
NSLog(@"%@", testString);
NSLog(@"testArray pointer: %p",&testArray);
NSLog(@"%@",testArray);
}
return 0;
}[/code]
Output
2014-02-08 21:35:58.176 Test on effect of ponters[21513:303] ***test 1***
2014-02-08 21:35:58.178 Test on effect of ponters[21513:303] testString pointer address: 0x7fff5fbff818
2014-02-08 21:35:58.178 Test on effect of ponters[21513:303] This is something
2014-02-08 21:35:58.178 Test on effect of ponters[21513:303] testArray pointer: 0x7fff5fbff810
2014-02-08 21:35:58.179 Test on effect of ponters[21513:303] (
"This is something"
)
2014-02-08 21:35:58.179 Test on effect of ponters[21513:303] ***test 2***
2014-02-08 21:35:58.180 Test on effect of ponters[21513:303] testString pointer address: 0x7fff5fbff818
2014-02-08 21:35:58.180 Test on effect of ponters[21513:303] say what?
2014-02-08 21:35:58.180 Test on effect of ponters[21513:303] testArray pointer: 0x7fff5fbff810
2014-02-08 21:35:58.181 Test on effect of ponters[21513:303] (
"This is something",
"say what?"
)
2014-02-08 21:35:58.181 Test on effect of ponters[21513:303] ***test 3***
2014-02-08 21:35:58.181 Test on effect of ponters[21513:303] testString pointer address: 0x7fff5fbff818
2014-02-08 21:35:58.181 Test on effect of ponters[21513:303] say what? this is how it is
2014-02-08 21:35:58.185 Test on effect of ponters[21513:303] testArray pointer: 0x7fff5fbff810
2014-02-08 21:35:58.185 Test on effect of ponters[21513:303] (
"This is something",
"say what? this is how it is",
"say what? this is how it is"
)
2014-02-08 21:35:58.186 Test on effect of ponters[21513:303] ***test 4***
2014-02-08 21:35:58.186 Test on effect of ponters[21513:303] testString pointer address: 0x7fff5fbff818
2014-02-08 21:35:58.187 Test on effect of ponters[21513:303] Back to square one
2014-02-08 21:35:58.187 Test on effect of ponters[21513:303] testArray pointer: 0x7fff5fbff810
2014-02-08 21:35:58.188 Test on effect of ponters[21513:303] (
"This is something",
"say what? this is how it is",
"say what? this is how it is",
"Back to square one"
)
Program ended with exit code: 0