##NSString versus C-String
NSString versus C-String
First and foremost, we can't call anything an Objective-C wrapper of a C library if we're expecting the end user to provide us with C-strings. If C-strings are necessary, we should allow our Objective-C end-users to provide us when an NSString
object which we will then convert to the appropriate C-style string at the appropriate time.
Presuming that these methods:
+ (BOOL)addUser:toGroup:
+ (BOOL)removeUser:fromGroup:
+ (BOOL)performOperation:withUser:andGroup:
are not in anyway exposed to the user, then your code successfully accomplishes the most important thing.
But that again presumes that these methods aren't in a .h
file anywhere or that you are compiling this library to a .a
file and not distributing the .h
file that contains these method declarations.
So, with that said, once we've received the NSString
object from the user, when should we make the conversion from NSString
to C-String
? The answer to that question depends on who is most likely going to be maintaining your code.
Will your code be maintained by Objective-C programmers who know just enough C? Or will your code be maintained by C programmers who know just enough about Objective-C?
Given that you seem uncertain about your C memory management, and you only actually use much C code in a single method (other than passing around C-style strings), I'm going to assume that you're primarily an Objective-C programmer who doesn't know a whole lot of C.
And in that scenario, we generally should be making the conversion at the last possible moment from Objective-C to C.
At any point in the future, some bug or some feature you want to implement might require you to do something with those strings in a particular part of the code. Aren't you going to be far more effective implementing and bug-fixing in Objective-C than you would be in C?
But the language of the maintainers isn't the only consideration. There's one final thing that also must be considered, and this final point is actually what completely decides whether we should use C-strings or NSStrings
for me. What are the performance implications?
Since we're not doing anything to the string besides passing it around until finally passing it into the C function, the only possible performance difference would be any sort of memory consideration.
All we're passing around in either case is a pointer, and that's going to be the same size in either case. Either only occupies the size of a pointer on the stack.
How about the heap?
If we create a C-String and an NSString
individually, assuming the same string, the NSString
will take up slightly more space on the heap--it has a little more overhead.
But in this scenario, that's not the only consideration. Creating the C-string doesn't make the NSString
go away. The NSString
already existed on the heap and continues to exist on the heap. So creating the C-string basically nearly doubles our heap memory usage for the time it exists.
So with that said, in order to improve our memory performance, we can simply, again, all together now, wait until the last possible moment before creating a C-style representation of our string.
##NSString versus C-String
First and foremost, we can't call anything an Objective-C wrapper of a C library if we're expecting the end user to provide us with C-strings. If C-strings are necessary, we should allow our Objective-C end-users to provide us when an NSString
object which we will then convert to the appropriate C-style string at the appropriate time.
Presuming that these methods:
+ (BOOL)addUser:toGroup:
+ (BOOL)removeUser:fromGroup:
+ (BOOL)performOperation:withUser:andGroup:
are not in anyway exposed to the user, then your code successfully accomplishes the most important thing.
But that again presumes that these methods aren't in a .h
file anywhere or that you are compiling this library to a .a
file and not distributing the .h
file that contains these method declarations.
So, with that said, once we've received the NSString
object from the user, when should we make the conversion from NSString
to C-String
? The answer to that question depends on who is most likely going to be maintaining your code.
Will your code be maintained by Objective-C programmers who know just enough C? Or will your code be maintained by C programmers who know just enough about Objective-C?
Given that you seem uncertain about your C memory management, and you only actually use much C code in a single method (other than passing around C-style strings), I'm going to assume that you're primarily an Objective-C programmer who doesn't know a whole lot of C.
And in that scenario, we generally should be making the conversion at the last possible moment from Objective-C to C.
At any point in the future, some bug or some feature you want to implement might require you to do something with those strings in a particular part of the code. Aren't you going to be far more effective implementing and bug-fixing in Objective-C than you would be in C?
But the language of the maintainers isn't the only consideration. There's one final thing that also must be considered, and this final point is actually what completely decides whether we should use C-strings or NSStrings
for me. What are the performance implications?
Since we're not doing anything to the string besides passing it around until finally passing it into the C function, the only possible performance difference would be any sort of memory consideration.
All we're passing around in either case is a pointer, and that's going to be the same size in either case. Either only occupies the size of a pointer on the stack.
How about the heap?
If we create a C-String and an NSString
individually, assuming the same string, the NSString
will take up slightly more space on the heap--it has a little more overhead.
But in this scenario, that's not the only consideration. Creating the C-string doesn't make the NSString
go away. The NSString
already existed on the heap and continues to exist on the heap. So creating the C-string basically nearly doubles our heap memory usage for the time it exists.
So with that said, in order to improve our memory performance, we can simply, again, all together now, wait until the last possible moment before creating a C-style representation of our string.
NSString versus C-String
First and foremost, we can't call anything an Objective-C wrapper of a C library if we're expecting the end user to provide us with C-strings. If C-strings are necessary, we should allow our Objective-C end-users to provide us when an NSString
object which we will then convert to the appropriate C-style string at the appropriate time.
Presuming that these methods:
+ (BOOL)addUser:toGroup:
+ (BOOL)removeUser:fromGroup:
+ (BOOL)performOperation:withUser:andGroup:
are not in anyway exposed to the user, then your code successfully accomplishes the most important thing.
But that again presumes that these methods aren't in a .h
file anywhere or that you are compiling this library to a .a
file and not distributing the .h
file that contains these method declarations.
So, with that said, once we've received the NSString
object from the user, when should we make the conversion from NSString
to C-String
? The answer to that question depends on who is most likely going to be maintaining your code.
Will your code be maintained by Objective-C programmers who know just enough C? Or will your code be maintained by C programmers who know just enough about Objective-C?
Given that you seem uncertain about your C memory management, and you only actually use much C code in a single method (other than passing around C-style strings), I'm going to assume that you're primarily an Objective-C programmer who doesn't know a whole lot of C.
And in that scenario, we generally should be making the conversion at the last possible moment from Objective-C to C.
At any point in the future, some bug or some feature you want to implement might require you to do something with those strings in a particular part of the code. Aren't you going to be far more effective implementing and bug-fixing in Objective-C than you would be in C?
But the language of the maintainers isn't the only consideration. There's one final thing that also must be considered, and this final point is actually what completely decides whether we should use C-strings or NSStrings
for me. What are the performance implications?
Since we're not doing anything to the string besides passing it around until finally passing it into the C function, the only possible performance difference would be any sort of memory consideration.
All we're passing around in either case is a pointer, and that's going to be the same size in either case. Either only occupies the size of a pointer on the stack.
How about the heap?
If we create a C-String and an NSString
individually, assuming the same string, the NSString
will take up slightly more space on the heap--it has a little more overhead.
But in this scenario, that's not the only consideration. Creating the C-string doesn't make the NSString
go away. The NSString
already existed on the heap and continues to exist on the heap. So creating the C-string basically nearly doubles our heap memory usage for the time it exists.
So with that said, in order to improve our memory performance, we can simply, again, all together now, wait until the last possible moment before creating a C-style representation of our string.
##NSString versus C-String
First and foremost, we can't call anything an Objective-C wrapper of a C library if we're expecting the end user to provide us with C-strings. If C-strings are necessary, we should allow our Objective-C end-users to provide us when an NSString
object which we will then convert to the appropriate C-style string at the appropriate time.
Presuming that these methods:
+ (BOOL)addUser:toGroup:
+ (BOOL)removeUser:fromGroup:
+ (BOOL)performOperation:withUser:andGroup:
are not in anyway exposed to the user, then your code successfully accomplishes the most important thing.
But that again presumes that these methods aren't in a .h
file anywhere or that you are compiling this library to a .a
file and not distributing the .h
file that contains these method declarations.
So, with that said, once we've received the NSString
object from the user, when should we make the conversion from NSString
to C-String
? The answer to that question depends on who is most likely going to be maintaining your code.
Will your code be maintained by Objective-C programmers who know just enough C? Or will your code be maintained by C programmers who know just enough about Objective-C?
Given that you seem uncertain about your C memory management, and you only actually use much C code in a single method (other than passing around C-style strings), I'm going to assume that you're primarily an Objective-C programmer who doesn't know a whole lot of C.
And in that scenario, we generally should be making the conversion at the last possible moment from Objective-C to C.
At any point in the future, some bug or some feature you want to implement might require you to do something with those strings in a particular part of the code. Aren't you going to be far more effective implementing and bug-fixing in Objective-C than you would be in C?
But the language of the maintainers isn't the only consideration. There's one final thing that also must be considered, and this final point is actually what completely decides whether we should use C-strings or NSStrings
for me. What are the performance implications?
Since we're not doing anything to the string besides passing it around until finally passing it into the C function, the only possible performance difference would be any sort of memory consideration.
All we're passing around in either case is a pointer, and that's going to be the same size in either case. Either only occupies the size of a pointer on the stack.
How about the heap?
If we create a C-String and an NSString
individually, assuming the same string, the NSString
will take up slightly more space on the heap--it has a little more overhead.
But in this scenario, that's not the only consideration. Creating the C-string doesn't make the NSString
go away. The NSString
already existed on the heap and continues to exist on the heap. So creating the C-string basically nearly doubles our heap memory usage for the time it exists.
So with that said, in order to improve our memory performance, we can simply, again, all together now, wait until the last possible moment before creating a C-style representation of our string.