![]() |
| | #1 |
| Registered User Join Date: Feb 2006
Posts: 58
| debug assertion failed 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 | |
| | #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 | |
| | #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:
| |
| Elysia is offline | |
| | #4 | ||
| Registered User Join Date: Dec 2006
Posts: 1,780
| Quote:
Quote:
| ||
| cyberfish is offline | |
| | #5 | |||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
Quote:
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:
| |||
| Elysia is offline | |
| | #6 |
| Guest 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 | |
| | #7 |
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| 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 | |
| | #8 |
| Kiss the monkey. 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |