Sign in to follow this  

Pass reference as pointer?

This topic is 369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,
I have a short question. Let's say I have the following two functions (pseudo code):

bool CreateBuffer(some vars, void *pData);

const std::vector& CMeshClass::GetVertexData();

Would it somehow be possible to pass the result of GetVertexData (const ref) to CreateBuffer as a pointer?

With this, assume that CreateBuffer can only take a ptr (no const ref, for valid reasons).

Any thoughts are appreciated.

Share this post


Link to post
Share on other sites

Two steps:

 

1)

bool CreateBuffer(some vars, const void *pData);

No matter what, you should bep assing pData in as const void anyways (I'm pretty sure you aren't going to manipulate the content of pData inside the function).

 

2)

const std::vector& vVertexData = mesh.GetVertexData();
CreateBuffer(..., &vVertexData);

Using &, you can create a pointer to vVertexData, and pass this to createBuffer.

Edited by Juliean

Share this post


Link to post
Share on other sites

Thanks, you're assuming on '1' is 100% correct (creating vertex buffers in this case, shouldn't modify the data).

The solution at 2 sounds pretty straight forward, thanks.

 

Btw; I need a void * / pointer in the CreateBuffer because I need the sizeof() to create the buffer.

Share this post


Link to post
Share on other sites

Btw; I need a void * / pointer in the CreateBuffer because I need the sizeof() to create the buffer.


That does not sound good, sizeof pData well just return the size of a pointer(4 and/or 8 depending on 32 bit/64 bit application), not the data structure. CreateBuffer should take a size argument. Edited by slicer4ever

Share this post


Link to post
Share on other sites

I know for sure that when using a template *, it works like this:

	template<typename T>bool Add(const T *pBufferData, unsigned int *pHandleCreated)
	{
		unsigned int newId = 0;
	
		// get free spot
		if(mFreeList.size() > 0)
		{
			newId = mFreeList.back();
			mFreeList.pop_back();

			mCBuffers.emplace_back();
		}
		else return false;

		// Create the constant buffer
		D3D11_BUFFER_DESC cBufferDesc;
		cBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
		cBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
		cBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
		cBufferDesc.MiscFlags = 0;
		cBufferDesc.StructureByteStride = 0;
		cBufferDesc.ByteWidth = sizeof(T);

Where sizeof(T) returns the size of the object T points too.

I'd hope/ think that it would work the same in the solution with void *pData (example/ question in this topic).

 

If not, you're absolutely right, then I would need to add a parameter to the function where I pass sizeof(pdata).

 

In case of templates, maybe the code is compiled/executed based on the type used as a template, and therefor it's a 'known type' returning a valid sizeof() instead of the pointer's size?

Edited by cozzie

Share this post


Link to post
Share on other sites
Thanks. Clear, I'll add the size as a parameter.
Another option would be to use a template here also, but because the buffer creation will still expect a void*, I might as well stay with the void* (combined with a size parameter).

Share this post


Link to post
Share on other sites

While you're staring at that nasty calculation, please take a moment to drop some support here:

size_bytes Proposal Discussion

 

 

It takes about 1 minute to make a templated function that is doing the same. It's a bad idea to extend STL, and add a new function to some of the containers, when the standard only has loose guarantees for the container's implementations.

Edited by sirpalee

Share this post


Link to post
Share on other sites
I think it's just a matter of choosing to pass the size also, or going for a template.
Both impementations are straight forward/ easy. From other feedback I got the idea that void* with size would be fine here, because the buffer will anyhow be created based on the unknown type/ block of memory (data).

Share this post


Link to post
Share on other sites

This topic is 369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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