Thread: Memory

  1. #1
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Memory

    I have a memory class that I really enjoy using. There are a couple of issues though:

    1) Is there a way to invoke the constructor and destructor manually on items returned from the pool such that the memory is not freed, just the functions called?

    2) if a user explicitly calls delete on an item, the items contents (or rather, the chunk within the pool) should not be freed since the underlying C/C++ lookup mechanisms won't find it to be a new'd/malloc'd pointer, correct?

    3) I am really trying to avoid overriding new/delete globally.

    4) I KNOW I can't override malloc/free, so I really hope assumption #2 is correct.

    Any thoughts?
    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;
    }

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Expand on "memory class" that's a bit vague...

    1) You can always explicitly call the destructor (assuming you declare it as public), just not the constructor. However, I wouldn't recommend you manually call it. Just make a separate function that performs destruction and just call it from the destructor (and make the destructor inline if it bothers you that much).

    2) I think you can only do that if you overload delete or make a completely different function that does what you want.

    3) If you really don't want to, why not just make your own versions of new and delete that don't have the names "new" and "delete." Like "allocate" and "deallocate."

    4) Why can't you overload malloc/free?

    I'm wondering if I'm not seeing exactly what you're trying to do, so maybe you can elaborate a bit on the problem, specifically what you mean by "memory class."

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793

    Re: Memory

    Originally posted by Sebastiani
    I have a memory class that I really enjoy using. There are a couple of issues though:

    1) Is there a way to invoke the constructor and destructor manually on items returned from the pool such that the memory is not freed, just the functions called?
    Although every one knows you can not explicitly call a constructor (You can call a destructor manually though), there is a way for you to simulate it;

    Code:
    #include <iostream>
    #include <new>
    
    class Foo {
    public:
    	int number;
    	Foo(int num) {number = num;}
    
    };
    
    
    int main()
    {
    	const int nNum = 10;
    	const int nVal = 2;
    	
    	//call global operator new[] to alloc
    	//memory without constructor calls
    	Foo* ptrFoo = static_cast<Foo*>
    		(operator new[](nNum*sizeof(Foo)));
    	
    	//call placement new to call constructors
    	//of each object in pre-alloced memory
    	for(int i = 0;i < nNum;i++)
    		new(ptrFoo+i) Foo(nVal*i);
    	
    	//Read value of each object...
    	for(int i = 0;i < nNum;i++){
    		std::cout << "Foo number " << i+1;
    		std::cout << " has a value of ";
    		std::cout << ptrFoo[i].number << std::endl;
    	}
    	
    	//Must now make call manual call to destructor..
    	//maybe not needed, but to make a point
    	for(int i = 0;i < nNum;i++)
    		ptrFoo[i].~Foo();
    	
    	//call operator delete[] to free memory
    	operator delete[] (ptrFoo);
      
    }
    Originally posted by Sebastiani
    2) if a user explicitly calls delete on an item, the items contents (or rather, the chunk within the pool) should not be freed since the underlying C/C++ lookup mechanisms won't find it to be a new'd/malloc'd pointer, correct?
    You need to overload operator delete to do the actual freeing, this can then alter the size of the free memory pool you have created.....the call to delete will call the object's destructor so you can leave that as is

    Originally posted by Sebastiani
    3) I am really trying to avoid overriding new/delete globally.
    You cant overload new - new covers calls to operator new(which can be overloaded and acts like malloc - void* return and all), placement new and a static_cast to the type required.......

    You can overload operator new though...

    Code:
    #include <cstdio>
    #include <new>
    #include <cstdlib>
    
    class foobar{};
    
    void* operator new(std::size_t size)throw(std::bad_alloc){
    	std::printf("%s","operator new called\n");
    	return std::malloc(size);
    }
    
    int main(){
    
    	int* x = new int; 
    		
    	foobar* foo = new foobar;
    	
    	delete x;
    	
    	delete foo;
    
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any thoughts?
    All of your problems appear to spawn from the fact that you use new and delete for your memory handling needs. Instead of doing this you might consider going the way of other low level libraries and making use of the standard allocators in <memory>. This gives you the control you need to allocate/deallocate raw blocks of memory and construct/destruct independently of the allocations. For more information you should consult Stroustrup (19.4, pp 567).

    -Prelude
    My best code is written with the delete key.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I guess I was a little vague. Essentially what I have is a class which allocates a block to serve as a heap:

    Memory heap(1000000);

    Now normally, I use the class for requsting chunks of native types only:

    int * n;
    char * c;

    n = heap.alloc< int >( 1000 ); //...request 4000 bytes
    heap.alloc< char >( c, 500 ); //...use garbage collection

    Now the problem is in non-natives with contructors:

    Foo * f = heap.alloc<Foo>(4); //...constructor not called.

    ...and destuctors...

    heap.free( f ); //...destructor not called.

    delete [] n; //...uh-oh, what happens now,

    free(c); //...and now?

    Now, having a little forsight, the first chunk obtained frm the heap object is guaranteed not to be the 0th offset. I hoped this would cause calls to delete/free to fail (after all, I don't want the system to attempt to free chunks from the middle of my heap!).

    So I guess the question is how to invoke a constructor without causing the system to allocate memory for it, and likewise, the destructor.

    Oh and thanks fordy for that example. What I truly would want would be to conserve the calling syntax, but I'm aware this probably isn't possible.
    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;
    }

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>So I guess the question is how to invoke a constructor without causing the system to allocate memory for it, and likewise, the destructor.

    That's pretty straightforward.....overload operator new and operator delete for the object...(They act just like malloc & free for when you allocate with new)...new will then call your overloaded operator new to allocate the raw memory (for which you may have memory pooled or have access to another heap)......new then takes the memory you have provided and constructs an object inside it......when delete is called, it calls the object's destructor then your operator delete will have the task of deallocating that memory - freeing it or adding it back to the pool....

    As in my example above you can overload operator new and operator delete globally....but you should be carefull when doing so...it may lead to problems

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM