CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 25

Threaded View

  1. #10
    Join Date
    Jun 2008
    Posts
    592

    Re: Declare class pointer with or without new

    of course minimizing memory leaks is very important, but if you can't keep your programming from leaking memory, c++ isn't right for you. the new operator in c++ plays a very important role in its design. polymorhpic classes wouldn't even be the same without new.

    Here is some stupid code
    Code:
    char* Str = "Wow";
    delete [] Str;
    isn't that just great? or
    Code:
    char* Str = new char[100];
    free( Str );
    or
    Code:
    char* Str = (char*) malloc( sizeof( char ) * 100 );
    delete [] Str;
    or
    Code:
    shared_ptr<char> Str ( new char[100] );
    or
    Code:
    template<class T> inline void destroy(T*& p) { delete p; p = 0; }
    char* Me = new char[100];
    destroy( Me );
    or
    Code:
    void m( char* p )
    {
        delete [] p;
        p = new[100];
    }
    All those examples will be undefined behaviour I seen worser code than memory leaks . How about memory corruption?

    Code:
    int t[100];
    t[100] = 0;
    Code:
    int *t;
    *t = 100;
    Code:
    char Str[] = {'H', 'e', 'l', 'l', 'o' };
    cout << Str << endl;
    c++ is not a safe language, so it use wisely. the list can go on for miles
    Last edited by Joeman; December 28th, 2009 at 08:25 AM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured