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. General Programming
  3. C / C++ / MFC
  4. the size of string

the size of string

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestion
3 Posts 3 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.
  • G Offline
    G Offline
    gucy
    wrote on last edited by
    #1

    when i trace the following code,the value of "size" is alwarys 4 instead of 14, why? int size; char *str; str = (char*)malloc(128); sprintf(str,"string1\ string2") size = sizeof(str); gucy

    C T 2 Replies Last reply
    0
    • G gucy

      when i trace the following code,the value of "size" is alwarys 4 instead of 14, why? int size; char *str; str = (char*)malloc(128); sprintf(str,"string1\ string2") size = sizeof(str); gucy

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      First of all, you know you're using C, not C++, right ? 4 is the size of a 32 bit number, i.e. the number which stores the memory address where your string lives. You're looking for strlen, I believe. Or use C++ instead, for example std::string or CString if you're using MFC ( or ATL in VC7, I believe ). Of course, if you're learning, it is indeed better that you understand what the string classes do for you, but sizeof is the wrong approach, it's strlen you want. Christian NO MATTER HOW MUCH BIG IS THE WORD SIZE ,THE DATA MUCT BE TRANSPORTED INTO THE CPU. - Vinod Sharma Anonymous wrote: OK. I read a c++ book. Or...a bit of it anyway. I'm sick of that evil looking console window. I think you are a good candidate for Visual Basic. - Nemanja Trifunovic

      1 Reply Last reply
      0
      • G gucy

        when i trace the following code,the value of "size" is alwarys 4 instead of 14, why? int size; char *str; str = (char*)malloc(128); sprintf(str,"string1\ string2") size = sizeof(str); gucy

        T Offline
        T Offline
        Toni78
        wrote on last edited by
        #3

        The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type. Which means that in your case your are getting the size of str which is nothing but a pointer. A pointer stores a memory location, which is only 4 bytes. If you truly want to get the size in bytes of your string that you should try this: size = sizeof(char) * strlen(str) // Afterall, I realized that even my comment lines have bugs

        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