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. using _ultoa_s is creating a problem

using _ultoa_s is creating a problem

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
10 Posts 5 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.
  • V Offline
    V Offline
    VCProgrammer
    wrote on last edited by
    #1

    Hi all, I am using _ultoa_s for conversion of long to string using this way

    _ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
    pListCtrl->SetItemText ( index, 2, (char *)Name);

    But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance

    N L M 3 Replies Last reply
    0
    • V VCProgrammer

      Hi all, I am using _ultoa_s for conversion of long to string using this way

      _ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
      pListCtrl->SetItemText ( index, 2, (char *)Name);

      But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance

      L Offline
      L Offline
      L Madhavan
      wrote on last edited by
      #2

      sizeof(&Name) is the culprit - the size of an address (pointer) is always 4 bytes. (for a 32-bit compiler) What type is the variable Name? If its a character array, you should use sizeof(Name). if it's a pointer, you need to know the size with which it was allocated.

      N 1 Reply Last reply
      0
      • V VCProgrammer

        Hi all, I am using _ultoa_s for conversion of long to string using this way

        _ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
        pListCtrl->SetItemText ( index, 2, (char *)Name);

        But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance

        N Offline
        N Offline
        Naveen
        wrote on last edited by
        #3

        VCProgrammer wrote:

        But the problem is sometimes its executing fine but sometimes it just crashes my application....

        I dont think this is an issue of _ultoa_s. It can also be an issue in accessing pDir32->Dir [ i ].fileSize variable. So first try to isolate it. Please change the code as follows and find out at which line the crash occurs.

        unsigned long lFileSize = pDir32->Dir [ i ].fileSize;
        _ultoa_s ( lFileSize ,(char *)Name,sizeof(&Name), 10 );

        nave [OpenedFileFinder] [My Blog]

        1 Reply Last reply
        0
        • V VCProgrammer

          Hi all, I am using _ultoa_s for conversion of long to string using this way

          _ultoa_s ( pDir32->Dir [ i ].fileSize,(char *)Name,sizeof(&Name), 10 );
          pListCtrl->SetItemText ( index, 2, (char *)Name);

          But the problem is sometimes its executing fine but sometimes it just crashes my application.... i am working in vc 2008.. Thanks in advance

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #4

          What is the type of Name ? May be sizeof(&Name) giving the problem.

          -Malli...! :rose:****

          1 Reply Last reply
          0
          • L L Madhavan

            sizeof(&Name) is the culprit - the size of an address (pointer) is always 4 bytes. (for a 32-bit compiler) What type is the variable Name? If its a character array, you should use sizeof(Name). if it's a pointer, you need to know the size with which it was allocated.

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            L. Madhavan wrote:

            sizeof(&Name) is the culprit

            :doh: I missed that point. How ever if the Name is allocated in the stack, there is no problem in using sizeof(Name).

            nave [OpenedFileFinder] [My Blog]

            modified on Wednesday, December 17, 2008 2:34 AM

            M 1 Reply Last reply
            0
            • N Naveen

              L. Madhavan wrote:

              sizeof(&Name) is the culprit

              :doh: I missed that point. How ever if the Name is allocated in the stack, there is no problem in using sizeof(Name).

              nave [OpenedFileFinder] [My Blog]

              modified on Wednesday, December 17, 2008 2:34 AM

              M Offline
              M Offline
              Malli_S
              wrote on last edited by
              #6

              But if Name is dynamically allocated on heap, sizeof(Name) and sizeof(&Name) both returns the size of pointer. Doesn't it ?

              -Malli...! :rose:****

              C N 2 Replies Last reply
              0
              • M Malli_S

                But if Name is dynamically allocated on heap, sizeof(Name) and sizeof(&Name) both returns the size of pointer. Doesn't it ?

                -Malli...! :rose:****

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                If Name is a plain string, use strlen instead. It is much safer. Note to self: take a morning coffee before answering questions... :sigh:

                Cédric Moonen Software developer
                Charting control [v1.5] OpenGL game tutorial in C++

                modified on Wednesday, December 17, 2008 3:17 AM

                L 1 Reply Last reply
                0
                • M Malli_S

                  But if Name is dynamically allocated on heap, sizeof(Name) and sizeof(&Name) both returns the size of pointer. Doesn't it ?

                  -Malli...! :rose:****

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #8

                  Malli_S wrote:

                  Doesn't it ?

                  Yes :)

                  nave [OpenedFileFinder] [My Blog]

                  1 Reply Last reply
                  0
                  • C Cedric Moonen

                    If Name is a plain string, use strlen instead. It is much safer. Note to self: take a morning coffee before answering questions... :sigh:

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    modified on Wednesday, December 17, 2008 3:17 AM

                    L Offline
                    L Offline
                    L Madhavan
                    wrote on last edited by
                    #9

                    strlen returns the current length of the string, not the actual size of the buffer. Moreover, if the buffer is uninitialized, (i.e. no null terminator) the behaviour of strlen is undefined.

                    C 1 Reply Last reply
                    0
                    • L L Madhavan

                      strlen returns the current length of the string, not the actual size of the buffer. Moreover, if the buffer is uninitialized, (i.e. no null terminator) the behaviour of strlen is undefined.

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      Oh crap, I forgot to take my coffee this morning X| Yes, of course, we need to pass the size of the available buffer, not the size of the string...

                      Cédric Moonen Software developer
                      Charting control [v1.5] OpenGL game tutorial in C++

                      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