Thread: Bizzare: Function Returning Junk

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    19

    Bizzare: Function Returning Junk

    This is serious stuff guys. Here is what I am working with.....

    I wrote the IString class and it works when tested but when this other guy uses my class for two of his applications the one application works and the other does not. This is the function he wrote and he is using it in both his applications:

    Code:
    char * user_name(){
      return (IString::upperCase(getlogin()));
    }
    It is called like this, see the code below, in both the applications but one works and the other returns junk. But when I print the value inside user_name they are there but they are not returned. Any ideas? I have treid 20 different things and I still can't get it to work.

    Code:
    char sCreateuser[15];
    char sUpdateuser[15];
    strcpy(sCreateuser,user_name());
    strcpy(sUpdateuser,user_name());
    cout<< sCreateuser <<endl;
    cout<< sUpdateuser <<endl;

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    23
    Obviously we would have to see both your IString class and both programs your friend used in order to get anywhere on this.--at least the important pieces of code from each.

    I'm thinking it's probably an error in your IString class although it could be an error in either of his pieces of software. Sounds highly likely like a buffer overflow. Test your IString class better.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    As stated above, are you sure those user names fit in a 15 character storage space? Rembember you need run for the null terminator too.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    Yes the name does fit and it is not a buffer overrun problem.

    Someone at work suggested that I might be missing the copy constructor. So I created one and I think that might be the problem but I don't think my copy constructor works as expected for I get a core dump. Here is my copy constructor. Note that buffer is just an STL string object declared as a member variable.

    Code:
    IString :: IString ( const IString &aString )
    {
    	buffer = aString.buffer;
    }
    Last edited by mangoMan; 05-25-2004 at 02:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 06:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM