Thread: This function keeps exploding!

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

    This function keeps exploding!

    Part of my balance program: here is the withdraw function. There is a logging feature in it. Whenever the logging feature runs I get "failed debug assertions" and a big 'n happy illegal operation. I think it has to do with array overbounds, but I cannot find what. Variables balance, amount, and TempBalance are doubles. Here's the code for the function (The problem starts after the "LOGGING" comment.)

    Code:
    double withdraw(double amount,bool UsedInCreation = false)//RETURN VALUES: 0.00-INFINITY = Success,  -2 = FAIL(not enough money),  -1 = FAIL(other error)
    	{
    		double TempBalance = balance;
    		char* LogMessage = new char[1];
    		char* TextBuffer = new char[35];
    
    		if(amount == -1)
    			balance = balance - balance;
    		else if(amount != -1 && amount < 0)
    			return 0;
    		else if(amount > 0)
    			TempBalance = TempBalance - amount;
    
    		if(TempBalance < 0)
    			return -2;
    		else
    		{
    			//LOGGING
    			if(UsedInCreation == false)
    			{
    
    				time_t TimeVar = time(NULL);
    				time_t* MyTime = &TimeVar;
    
    				strcat(LogMessage,ctime(MyTime));
    				strcat(LogMessage," | WITHDRAWL | ");//NOTE: 11 chars between bars
    
    				sprintf(TextBuffer,"%f",balance);
    				CutToTwo(TextBuffer);
    
    				strcat(LogMessage,TextBuffer);
    				strcat(LogMessage," - ");
    
    				sprintf(TextBuffer,"%f",amount);
    				CutToTwo(TextBuffer);
    
    				strcat(LogMessage,TextBuffer);
    				strcat(LogMessage," = ");
    
    				sprintf(TextBuffer,"%f",TempBalance);
    				CutToTwo(TextBuffer);
    
    				strcat(LogMessage,TextBuffer);
    				strcat(LogMessage,"\n");
    
    				//cout << LogMessage;
    			}
    			else
    			{
    			}
    			//END LOGGING
    
    			balance = TempBalance;
    		}
    		
    		return balance;
    	}
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    How about a little more room for LogMessage?
    > char* LogMessage = new char[1];
    That's certainly not enough...
    char LogMessage[256];

    Or, you could do things the C++ way and just use a string object.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    LogMessage was very long before, but then when I tied to display it, it showed that much gibberish, and then the rest of the data, lind of in a scrambled error-prone way. Wait... ok.... I just compiled the project in release mode and everything went well. Damn that debugger Any explanations?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You are writing to memory you dont have. Listen to Eibro. The problems have not gone away in release mode just your debugger cannot tell you now. Your code will most likely work intermittently if at all. You cannot expect to ask for 1 byte space and write much more than that and for your code to not be broken.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by CodeMonkey
    LogMessage was very long before, but then when I tied to display it, it showed that much gibberish, and then the rest of the data, lind of in a scrambled error-prone way. Wait... ok.... I just compiled the project in release mode and everything went well. Damn that debugger Any explanations?
    Then memset() it to 0, like memset(reinterpret_cast<void*>(LogMessage), 0, sizeof(LogMessage)); and then try strcat'ing stuff on. I'm pretty sure strcat() searches for the first NULL in your string, so if it's not initilized there's no telling where the first NULL will be.

    OR take my advice and use a std::string object.

  6. #6
    I've never seen a function explode, can you show me?

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    It's like taking out a card in a card house. You add or take away a small bit of code in a closed program to find that your whole program folds over into chaos. It's entropy for geeks!
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    And yes, I expanded the buffer size greatly and set the execution char at [1] I think.. and that fixed my booboos for now.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    And we are.... KOSHER! No more resposes necessary. Thank you all.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM