Reputation: 5211
I have a class that has a private unsigned char * to a data buffer. The data buffer can be of varaible length, so I use malloc() and free() to allocate the amount of memory I need to hold the data.
My problem is that I have another class that needs to access this data. The way I'm currently doing so is by creating a working copy of the buffer and passing that into the other class. That is, I have a function get_data(unsigned char * copy, int size) that copies size bytes into the buffer specifed by copy. The buffer is small (~50 bytes), but I have to do this ALOT over the course of my program. As a result, I'm looking for a way that I can make this more streamlined.
Is there a way that I can pass the data buffer pointer to any other classes? Will they be able to overwrite the data in the buffer? I'm aware that I can send back a const copy of the data buffer pointer, but the caller can then call const_cast and modify it at will. That is, they can call const_cast and then something along the lines of buf_ptr[2] = 0xFF;
Thanks in advance for the help. I'm hoping that there is a way I can just use the pointer without the possiblity of a caller to corrupt the data if they do something nasty.
Upvotes: 0
Views: 1220
Reputation: 78459
Here's two solutions, both of which simple and will work:
1) just use classic getter and setter methods for the individual array. So that you iterate through an array by accessing get(0), get(1), etc. And setting works the same way. Problem is, it's going to have function call overhead (unless perhaps it's an inline function? Might be pretty optimized) and also you'd have to heavily restructure your program for using the array.
or, simply
2) Have something returning a pointer to the array. The private data will still be able to be modified using the pointer. So simply return a pointer to the array, and then everything else after that (setting and retrieving elements) will be fast and simple.
So, if you want to make it so that array is modifiable by the other functions, pointers would be your best bet. If not, then a simple "get" operator, declared inline for speed, and possibly overloading the [] operators if you feel so inclined, would work nicely.
Upvotes: 0
Reputation: 103733
I would rather just return a const unsigned char *
like paddy mentioned. But another option is to just give them access to individual elements of the array with an index. For example:
unsigned char get_data(size_t index) const
{
// you can even put a bounds check in here if you want
return m_private_buffer[index];
}
Or you could overload operator[]
for this.
Upvotes: 0
Reputation: 409256
There are two ways of handling this:
std::shared_ptr
for the data.std::vector<char>
for the data.Then create a "getter" function which returns either the shared pointer (in the first case) or a reference to the vector (in the second case). If you don't want the caller to modify the data then you can make the return value const
.
Upvotes: 0
Reputation: 63481
Just provide a const unsigned char *
accessor:
const unsigned char * MyClass::get_buffer() const { return m_private_buffer; }
Sure, there is a chance that someone using the buffer might decide to const_cast
and modify it, but that's not your problem. That's them doing something that they know is naughty. And indeed, if you are the one writing all the code, then why on earth would you try to break it?
Upvotes: 2