• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jerrinx

Bizarre placement new[] operator return address

11 posts in this topic


Hey Guys,
 
I am trying to do in place initialization of an array of variables.
Essentially allocate memory. Pass in the void * into operator new[] to initialize an array of variables.
 
Now I get an address 4 bytes ahead of the allocated address !!
 
Example:
void *pMem = malloc(sizeof(A) * 4);
A *pArr = new (pMem) A[4];
 
pArr is (char *)pMem + 4 !!
 
Can anyone shed light on why ?
I noticed the first 4 bytes of pMem contains the number of elements in array. (in this case 4).
 
I could potentially move the pointer back and forth. But I want to know is this 4 Bytes same for any platform ? Is it same for 32 bit and 64 bit applications ??
Is this part of the C++ standard ?
 
Thanks !!
Jerry
 
/*********** Source Code *********/
class BaseString
{
enum EAllocationSource {AS_DATA, AS_HEAP};
 
EAllocationSource m_allocationSource;
 
union
{
const char *m_strStatic;
char *m_strDynamic;
};
 
unsigned int *m_pNumRef;
unsigned int m_len;
 
static const char m_nullString;
 
public:
 
virtual ~BaseString(){}
 
static void *operator new[] (size_t size, void *ptr)
{
printf("\nSize obtained in operator new[] function of class: %u", (unsigned int)size);
return ptr;
}
 
static void operator delete[] (void *val, void *ptr)
{
printf("\nAddress passed to delete in Class: %u", (unsigned int)val);
printf("\nIn Place Address passed in Class: %u", (unsigned int)ptr);
}
 
};
 
void main()
{
printf("\nSize to be allocated: %u", sizeof(BaseString) * 4);
void *pMem = malloc(sizeof(BaseString) * 4);
printf("\nAllocated Address: %u", (unsigned int)pMem);
 
BaseString *pArr = new (pMem) BaseString[4];
printf("\nReturned Address: %u", (unsigned int)pArr);
 
pArr->operator delete[](pArr, pMem);
 
printf("\nuint first: %u", *((unsigned int *)pMem));
 
while(true)
SwitchToThread();
}
0

Share this post


Link to post
Share on other sites

Because A is a class, casting from void * to A * may involve shifting of the address, especially if it inherits from another class.

 

 

L. Spiro

-2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Hey Phil,

 

Your damn right.

I could have just used placement on each element !!

I R IDIOT :)

 

Thanks for the link too !!

 

Regards,

Jerry

0

Share this post


Link to post
Share on other sites

When new-ing an array of non-PODs, the compiler needs to store how many elements are in that array, so that it know how many destructors to call on delete. This value is often stored in memory before the array. By extension, this also means that it will allocate a bit more memory than N times sizeof(element), so your original placement new might write over the end of your buffer.

 

This behaviour is compiler specific, the best advice is to stay away from placement new for non-pod arrays! :)

If you can't do that, you may need to adapt your implementation for each compiler, so it might be easier to use a vector with a custom allocator.

2

Share this post


Link to post
Share on other sites

@L. Spiro.
A *pArr = new (pMem) A[4]; is not casting operation. It goes through operator new[] overloaded function.[/size]
It is used when you want to initialize an array of elements (calling constructors) given pre-allocated memory.[/size]

Erm, I know what placement new is and what it does.
And yes, it is implicitly performing a cast. You passed a void * and got back an A *. The address you passed was cast to A *. Shifting of addresses can happen in these cases, as I mentioned, specifically if A inherits from another class.
 

When new-ing an array of non-PODs, the compiler needs to store how many elements are in that array, so that it know how many destructors to call on delete. This value is often stored in memory before the array. By extension, this also means that it will allocate a bit more memory than N times sizeof(element), so your original placement new might write over the end of your buffer.
 
This behaviour is compiler specific, the best advice is to stay away from placement new for non-pod arrays! smile.png
If you can't do that, you may need to adapt your implementation for each compiler, so it might be easier to use a vector with a custom allocator.

This is the correct answer in this case.


L. Spiro
-1

Share this post


Link to post
Share on other sites
Huh, I've been C++ for over a decade, and am comfortable with dangerous constructs like placement new, but I've honestly never heard of placement new[]! Learn something new every day... From the sounds of some stack-overflow posts, it's not very useful though, and you should prefer the non-array one.
0

Share this post


Link to post
Share on other sites

Actually me too.

I have never even thought of using placement new with an array.  Never seen it before, never considered using it, never considered that it existed.

 

 

L. Spiro

0

Share this post


Link to post
Share on other sites
For those who care, the array allocation overhead is described as applying to placement new[] in section 5.3.4 paragraph 12 of the C++ standard (same location in all versions).
1

Share this post


Link to post
Share on other sites

Aside from trying to be Klever™ I suspect what the OP is doing is trying to reinvent either std::array or std::uninitialized_fill_n().  Perhaps he'd be more productive trying to use the language instead of modify it (although there's something to be said for discovering how your catalytic converter works instead of driving your car, too).

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0