none
Enumerate a StringBuilder as a char* RRS feed

  • Question

  • I have a StringBuilder variable that I would like to pin and enumerate using a char*.

     

    I know that when using a P/Invoke to unmanaged code you can use StringBuilder in place of the unmanaged char*. So it should be possible to use a char* in place of StringBuilder in managed code right?

     

    However, it doesn't work like a string, I can't use a StringBuilder like this:

    string str = "some text here";
    fixed (char* c = str)
    


    So is there some way I can get a StringBuilder as a char*?

    Saturday, October 23, 2010 6:18 PM

Answers

  • My observations were based on what you can see by opening mscorlib 4.0.0.0 with Reflector. The StringBuilder included there uses a buffer separated in chunks, stored as a chain of StringBuilders.

    HTH
    --mc

     

    • Marked as answer by SamAgain Sunday, October 31, 2010 2:48 PM
    Sunday, October 24, 2010 3:38 PM
  • Short answer: no.

    Long answer: a StringBuilder does not use a single array as a buffer: its contents can be represented by a chain of StringBuilders, which make it unsuitable to be converted it to a char*. What happens with P/Invoke is that the marshaller takes care of copying the StringBuilder to and from an unmanaged buffer it allocates and this is what makes the conversion possible.

    The only way would be to do something similar on the managed side, converting to a string or copying to a char array, but I guess this would defeat the whole purpose.

    HTH
    --mc

    • Marked as answer by SamAgain Sunday, October 31, 2010 2:48 PM
    Saturday, October 23, 2010 10:08 PM

All replies

  • Short answer: no.

    Long answer: a StringBuilder does not use a single array as a buffer: its contents can be represented by a chain of StringBuilders, which make it unsuitable to be converted it to a char*. What happens with P/Invoke is that the marshaller takes care of copying the StringBuilder to and from an unmanaged buffer it allocates and this is what makes the conversion possible.

    The only way would be to do something similar on the managed side, converting to a string or copying to a char array, but I guess this would defeat the whole purpose.

    HTH
    --mc

    • Marked as answer by SamAgain Sunday, October 31, 2010 2:48 PM
    Saturday, October 23, 2010 10:08 PM
  • Thanks for your reply.

    Obviously I knew of the option of StringBuilder.ToString and then converting to a char*. But you're right that would defeat the purpose since that would create an unnecessary memory allocation. But if that is what P/Invoke is doing then there is no difference anyways.

    Although I beg to differ on how the StringBuilder is stored. If you look into the ms repository you'll find that StringBuilder has a managed string as its base which is a single char array. What makes it faster than a string is that the StringBuilder tries to perform operations within its base string that has already been allocated and only if the contents can't fit then tries to reallocate. Whereas a normal string just reallocates on every operation.

    But I just realized that if I'm going to use pointers anyways then why worry about a StringBuilder.

    Saturday, October 23, 2010 10:29 PM
  • My observations were based on what you can see by opening mscorlib 4.0.0.0 with Reflector. The StringBuilder included there uses a buffer separated in chunks, stored as a chain of StringBuilders.

    HTH
    --mc

     

    • Marked as answer by SamAgain Sunday, October 31, 2010 2:48 PM
    Sunday, October 24, 2010 3:38 PM
  • I'm sorry, I was looking at version 2.0. Which doesn't split the data but does still have the speed with operations, I see what you mean about version 4.0.
    Tuesday, November 2, 2010 11:07 PM