Thread: Used to be Say what is going on with this edit field?

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    yeah, im a real malloc() person, because i was really into C. i have used localalloc and globalalloc, but its annoying to have to declare a handle and a pointer. but to use new, how would you declare a pointer to an array of chars? like this?

    Code:
    char *chBuffer=new char[256];
    and whats the difference between that and malloc()?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    new automatically allocates enough memory for a compound type, (a class for example), with malloc() you would need to calculate the size of the class then allocate the right amount of storage - AND REMEMBER to change that value when you change the class definition, or use sizeof() all over the place!

    new also returns a correctly typed pointer, malloc() always returns a void pointer which you need to cast.

    With new, you can initialise the object being allocated, not the case with malloc().

    new should throw an exception if it fails rather than return a void pointer which then needs to be checked. I say should because although that is the ANSI standard action, I've never used a PC compiler which did that. The C++ compiler on our Alpha servers does.

    I think best of all, you can override new and delete.

    Both work, one is newer than the other, and is ANSI standard for C++, malloc() is retained in C++ for compatibility reasons.

    Use one OR the other, you can get into trouble if you mix them in the same program.

    >>> how would you declare a pointer to an array

    An array is allocated as...

    TypedPointerVar = new VarType [size];

    ...and deleted like this...

    delete [] TypedPointerVar;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    how do you override those functions? i mean, i know how to override normal functions, but the syntax of new and delete is different from usual.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> how do you override those functions?

    new and delete are not functions, they are operators, and as such, can be overloaded relative to a class like any other. The skeleton for the overloaded functions are...

    Code:
    void *operator new (size_t size)
    {
        // Do whatever you want, allocate memory and throw a bad_alloc exception if necessary
        return PointerToMemory;
    }
    
    void operator delete(void *P)
    {
        // Do whatever you want, deallocate the memory
    }
    ... the constructor and destructors are called automatically.

    *** EDIT ***

    This is not Windows specific by the way, that is standard C++.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    are the two overrides you quoted there ones that will apply to any new and delete command? and when you say that they can be overloaded relative to a class, how does that work? is it like this?

    Code:
    void *operator myClass::new(size_t size)
    {
    }
    
    void operator myClass::delete(void *ptr)
    {
    }
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I have split this stuff from Dark Vipers stuff, as the thread had rapidly developed into two topics.

    >>> will apply to any new and delete command?

    No. Operator overloading is always relative to a class. If you overload new for class x, then a new x will use the overloaded class member new. new y would use the standard new operator, (assuming of course, new has not been overloaded in class y!).

    >>> is it like this?

    Yes.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capture Enter key press in EDIT Box
    By richiev in forum Windows Programming
    Replies: 4
    Last Post: 07-14-2005, 12:03 AM
  2. Buttons + Edit Control
    By jay kay in forum Windows Programming
    Replies: 6
    Last Post: 03-09-2005, 05:36 PM
  3. Subclassed edit not doing what it's supposed to
    By tyouk in forum Windows Programming
    Replies: 8
    Last Post: 01-21-2005, 10:25 PM
  4. Say what is going on with this edit field?
    By DarkViper in forum Windows Programming
    Replies: 33
    Last Post: 01-14-2003, 10:08 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM