|
|
Home » U++ Library support » U++ Core » StringBuffer size [BUG]
|
Re: StringBuffer size [BUG] [message #19340 is a reply to message #19338] |
Thu, 27 November 2008 20:09   |
bytefield
Messages: 210 Registered: December 2007
|
Experienced Member |
|
|
I think it's not a bug. You create a StringBuffer which store 128 chars and they are initialized with FreeFreeFree... to make you know when you use it without being initialized with something. It's easier to debug. Operator << is appending to the end of buffer, so that's why you get FreeFree stuff and also your chars.
If you want to make a StringBuffer initialized for 128 chars and want then to put your chars in it you have to call sb.Clear() to clear the initial content.
Also note that the buffer is filled with FreeFree... just when you have a debug build of your application.
cdabbd745f1234c2751ee1f932d1dd75
|
|
|
|
|
Re: StringBuffer size [BUG] [message #21557 is a reply to message #21412] |
Tue, 26 May 2009 14:44   |
piotr5
Messages: 107 Registered: November 2005
|
Experienced Member |
|
|
luzr wrote on Tue, 19 May 2009 07:27 | But that would defeat the purpose of StringBuffer. You are using the constructor variant with len parameter mostly if you need to "convert" external data to String, for example in LoadFile.
|
what does this mean? how do I call the constructor of StringBuffer at the exact location where my data is already stored in another container? if it were stored in a string-buffer then I could just pick this, then I don't need a constructor with len-parameter. are there any examples of re-interpreting some memory-position as a string-buffer of certain size? I really do not understand why StringBuffer actually needs a constructor with length-parameter!
a related idea, a possibility which is missing in U++. it is something similar to the array-type in c. we have vector and similar containers which basically are just pointers to some data along with the necessary beaurocrazy. what I am missing is a class which I could just sort-of reinterpret_cast to and from char[]. I imagine a templated class like:
template <class T,int L> class StaticArray
{...
T t[L];
...};
StaticArray<int,5> primes={{2,3,5,7,11}};
with all the pick-constructors, conversions and stuff. is there any library providing such a basic storage-class? I can imagine this would provide some opportunity for range-checking, and if the class T would provide a constructor alike T(0), then even a length could be defined as either zero-terminated or in case of zero-less StaticArray as L. I think in some badly written book on C I read that a "char s[50]" passed as a parameter to string-functions will be treated as zero-terminated, and in case of no zero it will default to the size 50. I've never seen this to work for C but it would be a nice feature for U++. is anyone interested in that kind of thing? but I guess I'm just curious how a pick-constructor could be implemented here. just imagine:
StaticArray<char,11> f1() {
StaticArray<char,11> out={"hello world"};
return out;
}
main() {
StaticArray<char,11> h=f1();
h[4]=' ';
Cout()<<f1()<<"\n"; //output: "hell world"
}
|
|
|
Re: StringBuffer size [BUG] [message #21558 is a reply to message #21557] |
Tue, 26 May 2009 15:04  |
cbpporter
Messages: 1427 Registered: September 2007
|
Ultimate Contributor |
|
|
AFAIK StringBuffer is meant to be a helper when one needs to interoperate with C like API that expects a preallocated buffer and a size for it. So instead of doing:
char* temp = (char *)malloc(MAXSIZE);
c_api_call(temp, MAXSIZE);
...
free(temp);
we can use StringBuffer to make it more safe. String Buffer also has pick semantics relative to String, so you can be convert it with zero cost if let's say you want to return the value of the buffer as a string from a function.
StringBuffer is not a class meant to build strings more efficiently like StringBuffer in Java or StringBuilder in C++. In U++ String plays the role of such classes, that's why it has a capacity.
It is also not meant to handle a raw pointer as a vector, though I find the idea very interesting. I think I saw somewhere a similar idea, but I don't remember where.
|
|
|
Goto Forum:
Current Time: Sat Apr 26 14:37:30 CEST 2025
Total time taken to generate the page: 0.00630 seconds
|
|
|