Passing char* to function ruins data?

This is a discussion on Passing char* to function ruins data? within the C++ Programming forums, part of the General Programming Boards category; Here's a project I just took off of the "waiting to die" pile. It is a super-simple money-managing program that ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    Passing char* to function ruins data?

    Here's a project I just took off of the "waiting to die" pile. It is a super-simple money-managing program that reads data line-by-line from a txt file. Everything is Shibby except for when the name of the current account is read in. It is read just fine, however, when I pass it to the label(char*) function, it dies. I cannot display the char* in the label function without an error. Here's the name-reading portion of the code and the account label function.
    Code:
    case NAME:
    			{
    				Reader.getline(buffer, 55);
    				if(Reader.eof())
    				{
    					active = false;
    					break;
    				}
    				cout << "DEBUG: Buffer at case <name> <ReadInAccounts()>:" << buffer << endl;//works
    				CurrentAccount.label(buffer);//Error at this call
    								SaveAccount(CurrentAccount);
    				read_state = MONEY;
    			}break;
    Code:
    int ACCNT::label(char *InputName)
    {
    	if(isnamed)
    		name = InputName;
    	else
    	{
    		name = InputName;
    		isnamed = true;
    	}
    	//cout << "DEBUG: name passed into <label()>" << InputName << endl;//DEBUG... causes a crash
    	//cout << "DEBUG: account name at <label()>: " << name << endl;//DEBUG... causes a crash
    return 0;
    }
    I'll note also, that if later in the program (after all of the data is read in) I try to access the name of the account, it is simply a blank string.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    got whitespace?

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Pardon?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    What is "name"...is it another char* ?

    If so, what do you think "name = InputName;" might be doing?

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What's buffer declared as?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    char* buffer = new char[100];

    and name is also a char*. The funny thing is, the argument in label() cannot be displayed without an error, but buffer can.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You cant use = to copy a char*....use strcpy() or better std::string - the latter way will get rid of the new/delete & resource management too

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Thank you, I'll do that- but my question is in that this causes an error:
    Code:
    cout << "DEBUG: name passed into <label()>" << InputName << endl;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by CodeMonkey
    Thank you, I'll do that- but my question is in that this causes an error:
    Code:
    cout << "DEBUG: name passed into <label()>" << InputName << endl;
    On executing "name = InputName" you are getting 2 pointers to the same string stored on the heap. So immediately after, you can pass both pointers to cout and get a valid string. But later I'm guessing you are calling "delete [] buffer" - this tells the heap manager that the memory for the string is no longer needed and can be use for whatever. So later if you pass name to cout, you are passing a pointer to deallocated memory - which is undefined and could very likely crash your system

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21