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

## Recommended Posts

Hello GameDevs ! I really feel I should be able to solve elementary stuff like this by now but due to my risk of finding a bad solution I wanted to hear how you guys would do it. I have a function to which I pass an unsigned char-pointer and I wish for the function to allocate memory for it while still maintaining its reference validity from outside the function.
BOOL CZip::GetFileData( string filename, UCHAR* pData, ULONG* pFilesize )
{
...
pData = new UCHAR[ info.uncompressed_size ]; // "info" is zlib stuff, works fine
...


Needless to say, it does not affect the target of the external pointer. I really have no clue how to achieve this but I reckon there's a simple solution I have missed (as usual). Many thanks in advance !

##### Share on other sites
You could return the pointer, and use NULL to signify errors.

A better way would be to pass an empty std::vector<UCHAR> reference, which the function can resize and fill. If you need to pass a raw pointer to the vector storage to zlib, you can use &vector.front() to obtain the pointer, which is perfectly safe as long as the library will only write vector.size() elements into it.

##### Share on other sites
Thanks a lot rip-off, both solutions are equally nice for this one :)

##### Share on other sites
Well, the second is much better with regards to ownership semantics.

##### Share on other sites
Due to the huge amounts of data I handle right now I was kind of hoping an elementary solution such as the standard array would do (I have a feeling it would provide a more efficient data management than a vector). This is why I'm a bit surprised that it wasn't easier to manage the array by reference.

##### Share on other sites
As long as you stick to resize(), the vector will not over-allocate its internal array, so it will behave identically to a manually managed primitive pointer, in terms of how much it allocates. std::vector<> is a an extremely lean wrapper around a raw array, it would be pretty much impossible to implement it any other way.

It is possbile to pass a pointer by reference, but its ugly and you gain little:
BOOL CZip::GetFileData( string filename, UCHAR *& pData, ULONG &pFilesize ){	...	pData = new UCHAR[ info.uncompressed_size ]; // "info" is zlib stuff, works fine	...}

##### Share on other sites
Eww, using a vector is a bit much. Pass it by reference.

##### Share on other sites
To the op, you code snippet should work. Try changing UCHAR *pData to UCHAR **pData and do the assignment like this *pData = new UCHAR[someSize];

##### Share on other sites
Quote:
 Original post by VanderryI have a feeling it would provide a more efficient data management than a vector.

Even if there is a measurable efficiency difference, it usually isn't worth the pain to handle raw arrays.

##### Share on other sites
Quote:
 Original post by pbryantEww, using a vector is a bit much. Pass it by reference.
A vector isn't always the right solution, to be sure, but can you explain why it would be a bit much in this circumstance? To me this seems like exactly the sort of situation where vector would be a good choice.
Quote:
 Original post by cgrantTo the op, you code snippet should work. Try changing UCHAR *pData to UCHAR **pData and do the assignment like this *pData = new UCHAR[someSize];
I'd use a reference rather than a pointer in this case. It's a minor detail, but IMO a reference would be cleaner and more expressive of intent (again, IMO).