Why do tutorials/examples use dynamically allocated memory?

Post Reply
Neurone
Posts: 7
Joined: Mon May 18, 2015 3:45 pm

Why do tutorials/examples use dynamically allocated memory?

Post by Neurone »

I've noticed that the tutorials and examples use dynamically allocated memory to create objects, instead of using the stack.
btCollisionShape* shape = new btBoxShape(btVector3(1, 1, 1));

What are the advantages of doing this instead of:
btBoxShape shape(btVector3(2, 2, 2));

I was having lots of trouble initially because I didn't realize that creating things on the heap did not ensure objects were properly aligned, causing the program to crash (maybe a note could be added in the tutorials about this)? Doing so on the stack also frees you from manually deleting them when you're done.
dern23
Posts: 26
Joined: Thu Oct 04, 2012 1:58 pm

Re: Why do tutorials/examples use dynamically allocated memo

Post by dern23 »

What compiler/platform are you using that doesn't support proper aligned heap allocation? Either way the reason for putting shapes, etc, on the heap is to avoid having to copy the object several times due to separate initialization and update functions and different associated scopes; we simply create the object once, and pass it's pointer around several functions, and delete it when the demo closes. It can be done in the way that you suggest, but at a cost of flexibility and more rigid design required.
Neurone
Posts: 7
Joined: Mon May 18, 2015 3:45 pm

Re: Why do tutorials/examples use dynamically allocated memo

Post by Neurone »

Vs2013 update 4. I get warnings about how using the new operator on something like btBoxShape will not garentee alignment.
dern23
Posts: 26
Joined: Thu Oct 04, 2012 1:58 pm

Re: Why do tutorials/examples use dynamically allocated memo

Post by dern23 »

I don't think that will make a difference unless BT_USE_SSE_IN_API is defined, which I believe is disabled by default on Windows. Maybe that's since changed or you set it yourself? Either way, if you really want the warning to go away, you need to override the 'new' operator for whatever class you are trying to instantiate. For Bullet, there is a predefined macro for this, place it in the public section of your class: BT_DECLARE_ALIGNED_ALLOCATOR().
Neurone
Posts: 7
Joined: Mon May 18, 2015 3:45 pm

Re: Why do tutorials/examples use dynamically allocated memo

Post by Neurone »

Yup, its still disabled on windows if i'm not mistaken.

I declared this outside the main function. Then I got this error while trying to compile:

error C2084: function 'void *operator new(size_t,void *) throw()' already has a body.

I think this is caused by other places already redefining this operator (maybe in <memory>). Is there an easy way to solve this problem?
dern23
Posts: 26
Joined: Thu Oct 04, 2012 1:58 pm

Re: Why do tutorials/examples use dynamically allocated memo

Post by dern23 »

It has to be in the class definition, like:

Code: Select all

struct BasicExample : public CommonRigidBodyBase
{
	BT_DECLARE_ALIGNED_ALLOCATOR()
	BasicExample(struct GUIHelperInterface* helper)
		:CommonRigidBodyBase(helper)
	{
	}
	virtual ~BasicExample(){}
	virtual void initPhysics();
	virtual void renderScene();
	void resetCamera()
	{
		float dist = 41;
		float pitch = 52;
		float yaw = 35;
		float targetPos[3]={0,0.46,0};
		m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
	}
};
Otherwise you are overriding the global new operator, which as you said is probably already overridden by some other header.
Post Reply