473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3464
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
4
by: Craig Kenisston | last post by:
Hi, I have a dll that must return a string to a C# .Net application and also needs to server a Delphi application. I have defined my delphi function like this : function GetString(var strPtr...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
2
by: m00nm0nkey | last post by:
Ok well i thought i'd try a different approach, so what I'm now trying is appending 50,000 lines from the collection to a stringbuilder, and then writing that entire stringbuilder to a file. ...
1
by: Jure Bogataj | last post by:
Hello! Does anybody knows how to handle this issue: I have an Delphi DLL with following two function declaration: function DeallocateString(lpszString : PChar) : DWORD; stdcall; function...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.