Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Obj-C string construction

Obj-C string construction

Scheduled Pinned Locked Moved The Weird and The Wonderful
learningswiftdata-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SirTimothy
    wrote on last edited by
    #1

    So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:

    char buffer[100];
    sprintf(buffer, "the value: %d", someValue);
    NSString* string = [NSString stringWithCString:buffer];

    of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:

    NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];

    B L 2 Replies Last reply
    0
    • S SirTimothy

      So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:

      char buffer[100];
      sprintf(buffer, "the value: %d", someValue);
      NSString* string = [NSString stringWithCString:buffer];

      of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:

      NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];

      B Offline
      B Offline
      Blake Miller
      wrote on last edited by
      #2

      I constantly observe the same type of constructions with the MFC CString, when the programer does not realize there is a .Format() function to do the same thing. I also crack up at the use of strlen and its variants to detect if the string is empty. How about just chekcing the first array slot for a zero! Or maybe they weren't after any kind of performance after all...

      B 1 Reply Last reply
      0
      • B Blake Miller

        I constantly observe the same type of constructions with the MFC CString, when the programer does not realize there is a .Format() function to do the same thing. I also crack up at the use of strlen and its variants to detect if the string is empty. How about just chekcing the first array slot for a zero! Or maybe they weren't after any kind of performance after all...

        B Offline
        B Offline
        BillW33
        wrote on last edited by
        #3

        I have known programmers who say that readability is more important than small performance gains. This doesn’t sound too bad until you realize that by “readable” they mean “using what they are familiar with” and not learning new, faster and better ways of doing things. Also the performance of their code is abysmal.

        Just because the code works, it doesn't mean that it is good code.

        1 Reply Last reply
        0
        • S SirTimothy

          So I was looking at some code I wrote a few years back, when I was first learning objective-C. I found many places where I did something like this:

          char buffer[100];
          sprintf(buffer, "the value: %d", someValue);
          NSString* string = [NSString stringWithCString:buffer];

          of course, with more useful text and values. I think there may have even been some times when I malloc'd buffer rather than having it on the stack... for those who aren't familiar with obj-C, this could be more sanely/appropriately written as:

          NSString* string = [NSString stringWithFormat:"the value: %d", someValue"];

          L Offline
          L Offline
          Le centriste
          wrote on last edited by
          #4

          The real horror here is that you don't check for the possibility of buffer overrun.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups