Sprintf memory leak ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nano2
    New Member
    • Jan 2007
    • 41

    Sprintf memory leak ?

    Hi,

    I am using sprintf like so where i want my numeric to be converted to a string
    char string[100];
    int result;
    ret = sprintf(string, "%d", result);

    does any one know if this will cause a memory leak ????


    Another question.

    is I have the following scenario -1234 is a numeric
    I want to convert this to a string like so "-1234"

    What would be the best c function to do this ????
  • willakawill
    Top Contributor
    • Oct 2006
    • 1646

    #2
    Originally posted by nano2
    Hi,

    I am using sprintf like so where i want my numeric to be converted to a string
    char string[100];
    int result;
    ret = sprintf(string, "%d", result);

    does any one know if this will cause a memory leak ????


    Another question.

    is I have the following scenario -1234 is a numeric
    I want to convert this to a string like so "-1234"

    What would be the best c function to do this ????
    hi you are unlikely to create a memory leak with this function unless you are using an integer with over 99 digits (which is impossible)
    In MS Visual c++ you can use a newer version, sprintf_s() which avoids overflow by including an extra parameter, the maximum characters to be written:
    Code:
    sprintf_s(string, 100, "%d", result);
    sprintf() will work for positive or negative numbers so:
    Code:
    sprintf(string, "%d", -1234);
    will work

    Comment

    • nano2
      New Member
      • Jan 2007
      • 41

      #3
      Originally posted by willakawill
      hi you are unlikely to create a memory leak with this function unless you are using an integer with over 99 digits (which is impossible)
      In MS Visual c++ you can use a newer version, sprintf_s() which avoids overflow by including an extra parameter, the maximum characters to be written:
      Code:
      sprintf_s(string, 100, "%d", result);
      sprintf() will work for positive or negative numbers so:
      Code:
      sprintf(string, "%d", -1234);
      will work
      Thanks fro that information..

      Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

      so for eg. result = 1234.5
      I want to have the following o/p " -1234.5" i.e the minus before the value ..
      Any idea how i can achieve this
      ????????????

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by nano2
        Thanks fro that information..

        Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

        so for eg. result = 1234.5
        I want to have the following o/p " -1234.5" i.e the minus before the value ..
        Any idea how i can achieve this
        ????????????
        spintf() will convert the sign as well as the numeric value, e.g. if you have code so
        Code:
            char string[100];
            int result =-1234;
            int ret = sprintf(string, "%d", result);
        string will contain "-1234"

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by nano2
          Thanks fro that information..

          Just one thing I want to append the minus sign onto the contents thats stored within the result variable.

          so for eg. result = 1234.5
          I want to have the following o/p " -1234.5" i.e the minus before the value ..
          Any idea how i can achieve this
          ????????????
          Either invert the value of the variable before using sprintf i.e.

          result = -result;

          or artifcially add a minus sign to the format string i.e.

          sprintf(string, "-%d", result);

          although this will only work for positive values of result negative values will result in 2 '-' signs in the output.


          Also note in you initial post you define result as an int, however in this post you have assigned 1234.5 to it which is not an integer value.

          Comment

          • nano2
            New Member
            • Jan 2007
            • 41

            #6
            Originally posted by Banfa
            Either invert the value of the variable before using sprintf i.e.

            result = -result;

            or artifcially add a minus sign to the format string i.e.

            sprintf(string, "-%d", result);

            although this will only work for positive values of result negative values will result in 2 '-' signs in the output.


            Also note in you initial post you define result as an int, however in this post you have assigned 1234.5 to it which is not an integer value.
            Thanks for your help
            if i use sprintf(string, "-%lf",result)

            for values like so 123 , 23214.23124 , 2414.2
            Will this cause meomy leaks do you know ?

            Thanks

            Comment

            • nano2
              New Member
              • Jan 2007
              • 41

              #7
              Can sprintf be used for long floats ?

              eg result = 4.999999
              sprintf(str, "-%lf", result);

              Comment

              • LSB
                New Member
                • Jan 2007
                • 8

                #8
                Originally posted by nano2
                Thanks for your help
                if i use sprintf(string, "-%lf",result)

                for values like so 123 , 23214.23124 , 2414.2
                Will this cause meomy leaks do you know ?

                Thanks
                sprintf will NOT cause memory leak if you will use it to convert regular C data types. No way. :)
                If you already have some memory leak, try to find another source of that.

                Comment

                • LSB
                  New Member
                  • Jan 2007
                  • 8

                  #9
                  Originally posted by nano2
                  Can sprintf be used for long floats ?

                  eg result = 4.999999
                  sprintf(str, "-%lf", result);
                  For Microsoft's compiler it will be:
                  f -- double Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision.
                  For e or E you will have exactly 3 digits after the decimal point.
                  They also have g and G format, described as being in fact
                  f or e format, whichever is more compact for the given value and precision

                  Comment

                  Working...