Sign in to follow this  

[C++] Bad array reference

This topic is 2848 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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by pbryant
Eww, 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 cgrant
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];
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).

Share this post


Link to post
Share on other sites

This topic is 2848 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