Re: about std::string

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



William wrote:
> {
> std::string str;
> char buff[4096];
> strcpy(buff, "HP Electric Co., Ltd.");
> str = buff;
> } //I get exception here which is "_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

Just for your info, that code is ugly but otherwise fine, because "HP..." is
not more than 4096 characters (why such a weird number, btw?).

> 2. if I just copy a string that the size is less than 15, there is no
> exception;

IIRC, that implementation uses a small, internal buffer (perhaps size =
16?). Only if the string doesn't fit there, it allocates memory
dynamically.

> 4. what is the meaning of _Bx._Buf & _Bx._Ptr of std::string?

I'm just guessing, one is the pointer to the allocated memory and the other
is the internal, fixed-size buffer. Expect _Bx to be a union of both.

I think the problem is not in your code but rather the way you compile it,
i.e. the environment.

Uli

.



Relevant Pages

  • Re: File Input Error Help
    ... This allocates memory for a pointer which is presumably intended to point ... allocate memory for the characters themselves. ... standard C++ string data type. ...
    (comp.lang.cpp)
  • Re: Clunky C cleanup code
    ... >I've written a function (clean_string) to remove characters from a string, ... >but it looks clunky to me, and I'm sure there's a more 'C' like way of ... /* Allocates memory and returns a string that is obtained by removing ...
    (comp.lang.c)
  • Re: Strings
    ... the memory usage won't increase very much ... >> because the new String and the existing StringBuffer will share ... >> the same big array of characters. ... copies the internal buffer on write. ...
    (comp.lang.java.programmer)
  • Re: memorymanager/fastmm question/suggestion
    ... would it make sense to have a FastSetLengthfunction, where blocksize is a 'hint' to fastmm for the blocksize to allocate/reserve. ... I'm aware of a simple solution that works just fine regardless of memory manager used. ... a string up to a particular size, you could reserve enough space so as the string increases in length, it becomes an in-place realloc, instead of a possible move.. ... As you append strings to it, it allocates a memory for the internal buffer in small chunks but only in cases when there is no room in the internal buffer for the new string being appended. ...
    (borland.public.delphi.language.basm)
  • Re: pointer calling trouble
    ... >> string. ... [snip - use it like this] ... This assumes that parseFilename allocates memory. ... Nothing about parseFilename suggests that it allocates memory. ...
    (comp.lang.cpp)