|
-
Oct 30th, 2001, 03:59 PM
#1
[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.
-
Oct 31st, 2001, 03:25 AM
#2
transcendental analytic
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.
-
Oct 31st, 2001, 06:46 AM
#3
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.
-
Oct 31st, 2001, 07:05 AM
#4
transcendental analytic
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.
-
Oct 31st, 2001, 07:18 AM
#5
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.
-
Oct 31st, 2001, 07:45 AM
#6
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.
-
Oct 31st, 2001, 07:48 AM
#7
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.
-
Oct 31st, 2001, 07:55 AM
#8
It just doesn't make sense
-
Oct 31st, 2001, 08:03 AM
#9
transcendental analytic
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.
-
Oct 31st, 2001, 08:23 AM
#10
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.
-
Oct 31st, 2001, 08:36 AM
#11
transcendental analytic
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.
-
Oct 31st, 2001, 08:41 AM
#12
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.
-
Oct 31st, 2001, 09:06 AM
#13
transcendental analytic
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.
-
Oct 31st, 2001, 09:26 AM
#14
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.
-
Oct 31st, 2001, 09:35 AM
#15
transcendental analytic
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.
-
Oct 31st, 2001, 10:28 AM
#16
Did you know about the set_new_handler()?
-
Oct 31st, 2001, 10:41 AM
#17
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()
-
Oct 31st, 2001, 11:43 AM
#18
transcendental analytic
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.
-
Oct 31st, 2001, 11:55 AM
#19
Of course its useful... thats why I posted it
-
Oct 31st, 2001, 01:11 PM
#20
Monday Morning Lunatic
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
-
Oct 31st, 2001, 01:20 PM
#21
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.
-
Oct 31st, 2001, 01:27 PM
#22
transcendental analytic
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.
-
Oct 31st, 2001, 02:42 PM
#23
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.
-
Oct 31st, 2001, 02:50 PM
#24
Monday Morning Lunatic
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
-
Oct 31st, 2001, 03:50 PM
#25
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|