C++ -- char * having junk characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girish16
    New Member
    • Mar 2008
    • 2

    C++ -- char * having junk characters

    Hi Experts,
    I am having problems with the below c++ code

    func manu()
    {
    char *Test1 = NULL;
    char *Test2 = NULL;
    int returnCode;

    // I pass Test1 to another function and populate its value;

    returnCode = function ( Test1 );

    Test2 = Test1;

    // when i print Test1 and Test2, sometimes they both have different values ( NOT ALWAYS, after i run them continuously for a couple of days )

    // Also sometimes, if I access Test2, the program crashes and generates core.

    // Please not, the above is part of a multithreaded program.

    delete Test1;
    delete Test2;

    }


    Could someone please tell me what is wrong with this code.
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    You talk about how you run the program. I find that hard to believe, given that the code won't compile. And it has runtime issues.

    For one thing, if you say func manu(), func is going to have be a valid type. I doubt it is. If it is, give us the declaration for func.

    Then, if you declare manu() to return a type, you must appropriately return the value in the code. Easy way to check, do you even use the return statement? No.

    delete must be paired with new. Where's your call to new?

    What does function(char*) do?

    Comment

    • girish16
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by oler1s
      You talk about how you run the program. I find that hard to believe, given that the code won't compile. And it has runtime issues.

      For one thing, if you say func manu(), func is going to have be a valid type. I doubt it is. If it is, give us the declaration for func.

      Then, if you declare manu() to return a type, you must appropriately return the value in the code. Easy way to check, do you even use the return statement? No.

      delete must be paired with new. Where's your call to new?

      What does function(char*) do?



      Apologies,
      My code looks something like this

      int manu()
      {
      char *Test1 = NULL;
      char *Test2 = NULL;
      int returnCode;

      // I pass Test1 to another function and populate its value;

      returnCode = function ( Test1 );

      Test2 = Test1;

      // when i print Test1 and Test2, sometimes they both have different values ( NOT ALWAYS, after i run them continuously for a couple of days )

      // Also sometimes, if I access Test2, the program crashes and generates core.

      // Please not, the above is part of a multithreaded program.

      delete Test1;
      delete Test2;

      return 0;
      }


      I complie it to an libtxn.so file and my web application will call this function.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Did you read Post #2??

        You cannot delete Test1 and Test2 becuse you never allocated them.

        Also, Test1 and Test2 are NULL. Accesssing a NULL will crash your program.

        Those functions you call do not alter Test1. I know that because you are not passing in the address of Test1. That means the function has a copy and it it can do is change the copy.

        Comment

        Working...