Thread: debug assertion failed

  1. #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)" ??

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    46
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    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.

    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...

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 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.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

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