C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-13-2009, 06:27 AM   #1
Registered User
 
Join Date: Feb 2006
Posts: 58
debug assertion failed

hi,i got the following code.it compiles welll.but when running,it gives a"debug assertion failed" error.


Code:
#include <iostream>

class test{
public:
	char *fileName;
	test(char *r){
		fileName=r;
	}
	~test(){
		delete(fileName);
	}
};
using namespace std;

void main(void){
	test r("xyz");

	system("pause");
}

wrong to have "delete(fileName)" ??
rahulsk1947 is offline   Reply With Quote
Old 06-13-2009, 06:37 AM   #2
Registered User
 
Join Date: Nov 2007
Posts: 29
Yep, in that case it is. You should probably use a std::string instead of a char* if it's an option, since it's much easier.

If it's not an option you should perhaps make the parameter in the constructor a const char*.

Maybe then consider allocating enough memory for the char *fileName to hold the content in r. Then copy the content of the r to the newly allocated memory that fileName points to, and then delete this memory in the destructor.

Not sure about the last part, like, if it's a good way to do it... But prefer using std::string over char* for strings.
JacobN is offline   Reply With Quote
Old 06-13-2009, 06:49 AM   #3
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
You can only delete what you new'ed. Because you didn't new the string, you cannot call delete on it. That's why you get the assertion.
Moreover, any string literal should be const char (http://sourceforge.net/apps/mediawik..._be_const_char).
You should not be using void main (http://sourceforge.net/apps/mediawik...itle=Void_main).
You can get rid of the "void" inside void main(void) too.
And an information article on T* vs T *: Stroustrup: C++ Style and Technique FAQ
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 06-13-2009, 07:33 AM   #4
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Quote:
You can get rid of the "void" inside void main(void) too.
Note that, if my memory serves me correctly, () has different meanings in C than in C++. In C++, () means no argument (same as (void)). In C, () in function prototype means unspecified arguments.

Quote:
And an information article on T* vs T *: Stroustrup: C++ Style and Technique FAQ
IMHO it unnecessarily confuses beginners. There are plenty of people doing it both ways, Beginners usually have more important things to worry about...
cyberfish is offline   Reply With Quote
Old 06-14-2009, 11:54 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by cyberfish View Post
Note that, if my memory serves me correctly, () has different meanings in C than in C++. In C++, () means no argument (same as (void)). In C, () in function prototype means unspecified arguments.
But this is C++, so it's unnecessary.

Quote:
IMHO it unnecessarily confuses beginners. There are plenty of people doing it both ways, Beginners usually have more important things to worry about...
I disagree. Many are just shown a particular style and thinks it's the only one.
It's important to choose a style you're comfortable with from the get-go and stick with it IMHO.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 06-14-2009, 12:00 PM   #6
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,923
>> But this is C++, so it's unnecessary.

Personally, I prefer to explicitly declare it void. But as you pointed out, these are just stylistic issues.
Sebastiani is offline   Reply With Quote
Old 06-14-2009, 01:25 PM   #7
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
Quote:
Originally Posted by rahulsk1947 View Post
wrong to have "delete(fileName)" ??
Yes, and wrong to have void main(void). Main must return int:
Code:
int main()
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 06-14-2009, 03:36 PM   #8
Kiss the monkey.
 
CodeMonkey's Avatar
 
Join Date: Sep 2001
Posts: 810
It must return an int, but will do so implicitly if you do not specify a return value. So even though you don't 'return', it's still important to make main() an int.
__________________
"If you tell the truth, you don't have to remember anything"
-Mark Twain
CodeMonkey is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
debug assertion failed! chintugavali C++ Programming 5 12-21-2007 04:05 AM
debug assertion failed ! blue_gene C++ Programming 2 05-09-2004 11:23 AM
Debug Assertion Failed! Ray Schmidt C++ Programming 3 02-21-2003 09:58 PM
Debug Assertion Failed minesweeper Windows Programming 5 12-11-2002 05:11 PM
Debug assertion failed Bajanine Windows Programming 4 10-17-2002 04:34 PM


All times are GMT -6. The time now is 07:48 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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