you could use std::string for that.
instead of a raw buffer and length, you can use std::string::data() and std::string::length().
Printable View
you could use std::string for that.
instead of a raw buffer and length, you can use std::string::data() and std::string::length().
std::string is pretty much contiguous in all possible implementations as far as I know due to its nature.
In C++11, it is also mandated in the standard that it be contiguous, so it suffices very well as a buffer.
You can use data() or c_str() to get a string to send, while using size() or length() to determine the length of the buffer.
Great, I'll have to rewrite all of my programs then. Lot of work but it sounds like it will simplify the code so it worth it.
Plus I heard something like that vector needs a contiguous memory to be allocated so maybe std::string will be more flexible and can allocate more memory.
Thank you.
it's really up to the operating system to decide how to allocate memory for it. if it needs a big contiguous block of memory, and there isn't one available, the memory allocation will simply fail. std::string behaves nearly identically to std::vector with respect to memory allocation.
Note that the "contiguous memory" is part of a virtual memory address space. For X86, virtual page size is 4k, and the physical 4k blocks can be scattered, but the operating system will map them to produce a contiguous virtual address space. How malloc(), new, free(), and delete work with an operating system isn't clearly defined, so I'm not sure what the limit is. For Windows XP 32 bit, I can usually allocate around 1.5GB with a single call to malloc(). For 64 bit systems, I'm not sure what the limit is.
It could be more than that. On Linux it would be bounded by your physical memory plus the swap partition. On Windows, the pagefile can automatically resize, so you might be able to allocate the entire free space of your hard drive of the windows partition.
Of course at that point you're accessing at hard-drive speeds, so you'd be better off using file IO directly or a database.
Long before that, for sufficiently large containers, continuous memory can be inefficient, so std::deque can be used. If you need random access with long strings, this might be viable.