Pass reference as pointer?

Started by
8 comments, last by cozzie 7 years, 4 months ago
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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

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.

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.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

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?

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

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

sizeof(T) will return the size of T, sizeof(T*) will return the size of a pointer (4 or 8 bytes), not the size of the total allocation pointed to by the pointer.

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

sizeof(void*) will work, and return the size of a pointer (4 or 8 bytes). sizeof(void) will not compile because void is an incomplete type, and the size of an incomplete type cannot be known.

If you need to know the size of what pData points to in the CreateBuffer function, you must pass it to the function. You don't have the information available to extract it from the parameter pData alone.

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).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me


std::vector<Vertex> stuff = mesh.GetVertexData();

size_t numBytes = stuff.size() * sizeof(decltype(stuff)::value_type);

function(stuff.data(), numBytes);

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

size_bytes Proposal Discussion

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

shaken, not stirred

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).

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement