Thread: Ok, this is unintelligible to me.

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    Unhappy Ok, this is unintelligible to me.

    Code:
    void* Point::operator new (size_t bytes)
    {
    	Block *res = freeList;
    	return used < maxPoints
    			   ? &(blocks[used++])
    			   : (res == 0	? 0 
    							: (freeList = freeList->next, res));
    }
    
    void Point::operator delete (void *ptr, size_t bytes)
    {
    	((Block*) ptr)->next = freeList;
    	freeList = (Block*) ptr;
    }
    I seriously hate to be a whiny little newbie, but this is just weird. I understand overloading in general, but I really don't recognize most of the variables in this as coming from anywhere.

    EDIT: Sorry about the little :o's, they are really a colon then a little o :rolleyes:

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>EDIT: Sorry about the little 's, they are really a colon then a little o
    When posting code like that, tick the "disable smilies" option. I've edited your post and done it for you this time.

    The variables are probably part of the class, maybe private members.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    28

    I get what you mean, but...

    They don't appear to be. What's odd to me is the very concept of overloading NEW. New strikes me as too basic to overload. That's what confuses me.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    There are several reasons. One is to use some sort of pool based memory, which it appears this code might be trying to do. Let's say the program is constantly creating and destroying Point objects all the time. All the calls to new and delete can lead to poor performance because each one has to allocate or deallocate memory in addition to constructing and destructing the Point object. Why not make one call to malloc that creates room for a whole bunch of Points, then always keep control of that memory. Whenever other code attempts to create a Point using new, the memory is already allocated, so the Point only needs to be constructed. When you are done with all the Points you deallocate the memory once. This can definitely improve performance depending on the situation.

Popular pages Recent additions subscribe to a feed