BOOL CZip::GetFileData( string filename, UCHAR* pData, ULONG* pFilesize )
{
...
pData = new UCHAR[ info.uncompressed_size ]; // "info" is zlib stuff, works fine
...
[C++] Bad array reference
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.
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 !
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.
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.
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.
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:
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 ...}
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];
Quote:Original post by Vanderry
I 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.
Quote:Original post by pbryantA 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.
Eww, using a vector is a bit much. Pass it by reference.
Quote:Original post by cgrantI'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).
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];
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement