Is there a way to determine the size of a char[] buffer via a char* pointer in C++? I'm using C++98.
The following lines of code all print out '8' (essentially sizeof(char*)). However I'm looking for the actual size of the buffer.
int maxBufferLen = 24;
char* buffer = new char[maxBufferLen];
std::cout << sizeof(buffer) << std::endl; // prints '8'
std::cout << ( sizeof(buffer) / sizeof(buffer[0]) ) << std::endl; // prints '8'
In practice, I am passing that char* buffer in between functions and I will not have access to the maxBufferLen variable that I am using to initialize it. As such, I'll need to determine the length using another way.
std::vector, andstd::stringexists. Without them, you have to keep track of all of those sizes.struct mybuf { char* buffer; size_t size; };and add a "few" member functions and free functions to go with it, you're home:std::string.sizeof(char), that by definition is1. You're gettingsizeof(char*)or the size of a pointer.