Results 1 to 25 of 25

Thread: [info] Calling an object's constructor

  1. #1
    Zaei
    Guest

    [info] Calling an object's constructor

    I found out a neat trick earlier. Consider the following code:
    Code:
    #include <iostream.h>
    #include <malloc.h>
    
    class c
    {
    public:
    	c();
    	~c();
    };
    
    c::c() {	cout << "c Constructed\n"; }
    
    c::~c() {	cout << "c Destructed\n"; }
    
    int main()
    {
    	c* p_c = NULL;
    	p_c = (c*)malloc(sizeof(c));
    	return 1;
    }
    In this code, the constructor of class c will never be called. How do we solve this problem. There is another new operator, called placement new. Consider:
    Code:
    #include <iostream.h>
    #include <new>
    #include <malloc.h>
    
    class c
    {
    public:
    	c();
    	~c();
    };
    
    c::c() {	cout << "c Constructed\n"; }
    
    c::~c() { 	cout << "c Destructed\n"; }
    
    int main()
    {
    	c* mem = NULL;
    	c* p_c = NULL;
    	mem = (c*)malloc(sizeof(c));
    	p_c = new(mem) c; //note how the placement new operator takes in a pointer to a memory address
    	return (unsigned long(mem)) == (unsigned long)p_c;
    }
    In the above, new takes in an extra parameter to a memory pointer, and constructs an object in that memory.

    I found this interesting, and I was looking for a solution to this problem a few weeks ago, and though someone might need it, or be interested.

    Z.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Smile neat

    So you say that I can call any object's X constructor T with:

    new(X) T;

    Thanks, that'll save me from typing init functions to reset new data, say a complex value new(X) cmplx(r,i); instead of X.SetRI(r,i);
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Zaei
    Guest
    It would construct class T's constructor in the memory pointed to by X. So, if you had an object allocated, in p_T, you could re-construct it with "p_T = new(p_T) T;".

    Z.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    or to avoid the unnessesary assignment, just
    new(p_T) T;
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    amac
    Guest
    You would have a memory leak... Thats not how placement new works... It still allocates memory like the new function. It just takes a parameter to where you want to construct the object.

    Code:
    void * memloc = 0xf39ab2cd;
    
    T * p_T = new (memloc) T;
    Its not for constructing objects in a pre-allocated block of memory.

  6. #6
    Zaei
    Guest
    Not in the example i saw.
    Code:
    class c
    {
    public:
       int x;
       int GetX() { return x; }
    };
    
    unsigned char mem[sizeof(c)];
    
    ...
    p_c = new(mem) c;
    ...
    So, obviously, it WILL allocate memory if you just give it a raw, un-allocated pointer, but if there is already memory there, it will use it.

    Z.

  7. #7
    Zaei
    Guest
    In fact, here is the definition of placement new, from the header:
    Code:
    inline void *__cdecl operator new(size_t, void *_P)
            {return (_P); }
    It simply returns the pointer you pass it, but new automatically calls the constructor.

    Z.

  8. #8
    amac
    Guest
    It just doesn't make sense

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I'd say your suggestion wouldn't make sense, since it's pretty useless to allocate some memory and call a construct somewhere else at the same time. Zaei's suggestion would be usefull for calling the constructor at least
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Zaei
    Guest
    It makes perfect sense. Your program can allocate some large block of memory for it's own use at the start of a program, and then use placement new to construct objects in that space.

    Z.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    btw I was talking to amac, Zaei, you mean that you use the stack as a buffer for for instance a queue, that would loop around the allocated memory? The copyconstructor would do work for variables while queueing constants would be done with new
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    amac
    Guest
    pretty useless to allocate some memory and call a construct somewhere else at the same time
    Thats not what I said.

    If someone asked you what new was... what would you say?

    "it allocates a block of memory on the heap. If the data type is not one of the primitive types then it calls the constructor of that object."

    So where does placement new allocate memory? So why exactly does what I said not make sense. Why would it not make sense to think that it would allocate memory and call the constructor. And why does it not make sense for something called _placement_ new would allocate memory at the _place_ you specified.

    I understand that I could... probably wrong, but this is how I understood it to be.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Smile memory leak

    I don't know, I'm new to this, placement new thing

    So you mean that placement new will allocate at the specified address? I would have understood you without context to my piece of code, but mentioning the memory leak i interpreted the a bit ambigous statement of yours as if you allocated the memory somewhere else without storing the location of it in a pointer. If the memory allocated is at specified address, how will it cause a memory leak?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    amac
    Guest
    I'm not really sure... the whole memory leak thing was a guess... maybe it wouldn't... but I would expect it would fail... when trying to allocate memory at a location where memory has alread been allocated...


    This is what I understood placement new to be... just to get things straight. It could be right... it could be wrong... I am not sure anymore...

    It still does the exact same thing as new does... execpt it takes an additional parameter to specify the memory location to create the object.

    This isn't something you would use very often. Only if you were doing some custom memory management stuff or even an OS Memory Manager.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Hmmm often I feel the lack of an expert at certain areas :/
    Somebody has to know how memory allocation on the heap is done
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    amac
    Guest
    Did you know about the set_new_handler()?

  17. #17
    amac
    Guest
    Code:
    int WeBeOutOfMemory ( size_t size )
    {
    
        cerr << "We be out of memory... Duhhhh!" << endl;
        abort();
    
    }
    
    int main(int argc, char* argv[])
    {
    
        _set_new_handler ( WeBeOutOfMemory );
    
        int * pBigDataArray = new int[1000000000];
    
        return 0;
    }
    Seems to work... although set_new_handler() can't be used... You have to use _set_new_handler()

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    Thumbs up

    wow! That was actually very usefull Thanks.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19
    amac
    Guest
    Of course its useful... thats why I posted it

  20. #20
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just to confirm...placement new constructs an object in memory you've already allocated - for example if you have a memory-mapped timer or something, you can construct the object onto it and magically your data members point to the timer
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  21. #21
    amac
    Guest
    I was waiting for you to post. Yah... I was talking to a few people here at work and thats what they were saying to...

    I can't remember where I read that it allocated it too.

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Okay, we can trust in parksie
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  23. #23
    Zaei
    Guest
    All back to what I said in the beginning =).

    kedaman, the idea of what I was talking about is like, you could malloc() something like 4 megs of memory, and then, overload new, so that, instead of actually allocating memory, it just gives you some from the store it allocated at the beginning. This would save some time if you had to allocate a lot of small memory blocks all at the same time.

    Z.

  24. #24
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yep, it was right first time but it ended up getting a little fuzzy half-way through.

    So you're talking about workspacing, right? Dunno what most people call it but that's what I've seen it referred to as in quite a few places.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  25. #25
    Zaei
    Guest
    I dont know what it is called, but thats the idea =).

    Z.

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