469,898 Members | 1,605 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

StringBuilder and PCHAR*

I am iterating through 454 rows of a table and with each row I use the ID
field to form a URI for a contact in a public folder on my exchange server.

My memory keeps growing though. I think it is because I reconstruct my URI
each time. I have done this 2 ways and I want to know something. How is
this ...

StringBuilder sb = new StringBuilder( 255, 255 );
for ( int x = 0; x < rs.Rows.Count; ++x )
{
if ( sb.Length > 0 ) sb.Remove( 0, sb.Length );
sb.AppendFormat( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][ "ID" ] );
...
}

any more efficient than this

for ( int x = 0; x < rs.Rows.Count; ++x )
string uri = string.Format( "{0}/{1}.EML", URI_PREFIX, rs.Rows[ x ][
"ID" ] );

The first snippet should be far more effective since I am pre-allocating my
buffer. However, since the ADODB.Connection object takes a string as its
connection source and string are immutable in dot net, whenever I call the
ToString method of the StringBuilder to give to the connection object, it is
going to allocate new memory for that string, correct?

I have found no way in C# to make sequential string operations occupy the
same memory. This is just plain wasteful.

Where in the hell then is the efficiency in this? Why is this better than C
or C++ where you can pass a pointer to an array of char so that you do not
re-create a string in memory from a buffer you pre-allocated for this very
reason to give to a function?

-a
Nov 15 '05 #1
4 3296
whenever I call the
ToString method of the StringBuilder to give to the connection object, it is
going to allocate new memory for that string, correct?


I can't find something to back it up at the moment, but I'm pretty
sure I've read that the runtime can optimize this by "detatching" the
memory used by the StringBuilder and turning it into a String, without
allocating new memory.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Mattias,

I think I know to what you refer. The StringBuilder does detach the buffer
to static memory when you call ToString the first time and each subsequent
call uses that formed string, but the documentation says if you "alter the
buffer" the next time you call ToString a new string will get created in
memory.

What it is unclear on is if that means the contents of the buffer, or the
size of the buffer.

Ideas?

-a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
whenever I call the
ToString method of the StringBuilder to give to the connection object, it isgoing to allocate new memory for that string, correct?


I can't find something to back it up at the moment, but I'm pretty
sure I've read that the runtime can optimize this by "detatching" the
memory used by the StringBuilder and turning it into a String, without
allocating new memory.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #3
In the words of Pink Floyd, "Is there anyone out there?"

-a

"Schley Andrew Kutz" <ak***@austin.utexas.edu> wrote in message news:<1D*******************@twister.austin.rr.com> ...
Mattias,

I think I know to what you refer. The StringBuilder does detach the buffer
to static memory when you call ToString the first time and each subsequent
call uses that formed string, but the documentation says if you "alter the
buffer" the next time you call ToString a new string will get created in
memory.

What it is unclear on is if that means the contents of the buffer, or the
size of the buffer.

Ideas?

-a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
whenever I call the
ToString method of the StringBuilder to give to the connection object, it isgoing to allocate new memory for that string, correct?


I can't find something to back it up at the moment, but I'm pretty
sure I've read that the runtime can optimize this by "detatching" the
memory used by the StringBuilder and turning it into a String, without
allocating new memory.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #4
In the words of Pink Floyd, "Is there anyone out there?"

-a

"Schley Andrew Kutz" <ak***@austin.utexas.edu> wrote in message news:<1D*******************@twister.austin.rr.com> ...
Mattias,

I think I know to what you refer. The StringBuilder does detach the buffer
to static memory when you call ToString the first time and each subsequent
call uses that formed string, but the documentation says if you "alter the
buffer" the next time you call ToString a new string will get created in
memory.

What it is unclear on is if that means the contents of the buffer, or the
size of the buffer.

Ideas?

-a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
whenever I call the
ToString method of the StringBuilder to give to the connection object, it isgoing to allocate new memory for that string, correct?


I can't find something to back it up at the moment, but I'm pretty
sure I've read that the runtime can optimize this by "detatching" the
memory used by the StringBuilder and turning it into a String, without
allocating new memory.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Alvin Bruney | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
1 post views Thread by Jure Bogataj | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.