Hey Phil,
Your damn right.
I could have just used placement on each element !!
I R IDIOT ![]()
Thanks for the link too !!
Regards,
Jerry
Male
jerrinx hasn't added any contacts yet.
12 April 2013 - 12:34 AM
Hey Phil,
Your damn right.
I could have just used placement on each element !!
I R IDIOT ![]()
Thanks for the link too !!
Regards,
Jerry
11 April 2013 - 11:43 PM
Hey guys,
Thanks for your replies.
@L. Spiro.
A *pArr = new (pMem) A[4]; is not casting operation. It goes through operator new[] overloaded function.
It is used when you want to initialize an array of elements (calling constructors) given pre-allocated memory.
@Martins
I agree that void* to A* will not change address.
If you notice the operator new[] is not changing the address. It is returned as such. Also in delete[] the value obtained is -4 of the address passed in (In other words the original address allocated). So the new[] operator is doing a +4 and delete is doing a -4. C++ is doing it !!
Try executing the code on your computer.
Also an update on my find:
I've tried the above code on a variety of different configurations. On Windows, On Unix and on A 64 Bit Application.
Here are my observations, when using In Place new[] operator overloading:
1. The memory returned from operator new[] is shifted forward by sizeof(size_t).
2. The memory passed as argument to delete[] is shifted back by sizeof(size_t).
3. The size argument to operator new [] will be equal to (sizeof(class) * numofArray) + sizeof(size_t).
4. The first size_t bytes of memory returned from new[] contains the number of elements allocated.
Also please note the above code needs to be changed to allocate +sizeof(size_t) memory to function without memory problems.
void *pMem = malloc(sizeof(BaseString) * 4 + sizeof(size_t));
I am wondering whether this is a C++ standard. If it is, I'd tweak the addresses myself and use it.
05 August 2012 - 09:04 PM
01 August 2012 - 02:08 AM
30 July 2012 - 12:25 AM
Find content