remove memory leak from this function strcpy

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ammarSyed
    New Member
    • Feb 2013
    • 2

    remove memory leak from this function strcpy

    Code:
    node_pointer *structures = new node_pointer;
     char buf[] = "dsdsdsdsdsds\0";
     structures->common_fields = buf;
    
    
      strcpy(structures>common_fields,"aaaaasfd\0");
      cout<<structures->common_fields;
    Syed Muhammad Ammar: memory leak error at strcpy function.....
    Last edited by zmbd; Feb 23 '13, 10:15 PM. Reason: [Z{Please use the <CODE/> formatting button to format your posted code and SQL}]
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    You need to provide more of your code.
    Best guess is that you've initialized the node and then don't release the allocation and then create a new node and then repeat this act over and over.
    Remember it is always good practice to ensure that all allocations are released when they are no-longer needed and whenever a fatal error occurs.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You create a new node_pointer each time. That's OK.
      But:
      Code:
      char buf[] = "dsdsdsdsdsds\0";
      structures->common_fields = buf;
      every one of your node_pointer gets the same address. namely, the address of buf. So each node copies to the same address.

      Unfortunately, when the function completes, buf is deleted (it's a local variable) and now all of your nodes are skrogged.

      Never use the address of a local variable in such a way that the address exists after the function completes and the variable it points at is gone.

      Comment

      • ammarSyed
        New Member
        • Feb 2013
        • 2

        #4
        memory leak warning strcpy

        Warning 1 warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_ WARNINGS. See online help for details. D:\Visual Studio WebApplications \test memory leak 1\test memory leak 1\test memory leak 1.cpp


        how to remove this warning........ .

        Comment

        • divideby0
          New Member
          • May 2012
          • 131

          #5
          the complier wants you to use strcpy_s instead of strcpy because of potential security issues, I think. you can disable the warning using something like below.

          Code:
          #pragma warning(disable : 4996)
          
          #include <stdio.h>
          #include <string.h>
          ... // other includes and code bits

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Your threads have been merged. Please do not post your questions more than once.

            Comment

            • ani627
              New Member
              • Mar 2013
              • 2

              #7
              Try strncpy or memcpy.

              Comment

              Working...