473,326 Members | 2,110 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,326 software developers and data experts.

System::String * and string literals

When we assign a managed or unmanaged string literal to a String *, is a new String
created implicitly in the managed heap?
Or in other words, what happens when we assign a string literal to TextBox::Text and then
writing in this TextBox?
Nov 17 '05 #1
5 1635
First of all, in unmanaged code, if you assign a constant string to a
char*:

const char* szText1 = "Hello";
const char* szText2 = "Hello";

compiler generates one "Hello" and both szText1 and szText2 point the
same location.

Very same rule applies to managed applications as well.

What happens when you "type" inside the textbox deals with Windows.
After all, it is a Windows "EDIT" control. Read GetWindowText on MSDN
to learn more about how windows manage their window texts.

So,

String* pString1 = "Hello";
String* pString2 = "Hello";

will point the same location - no new string is created for pString2.
However, a new String object is constructed, and these two pointers
will be pointing to that object. (Write a sample, and open
disassembler, see "mov ecx,.." sections. ECX is "this" pointer.)

Any new assignment will change the pointer.

String* pString = "Hello";
// somewhere in your application
pString = textbox->Text; // pString points to a new object - old one is
still in GC heap!!
textbox->Text = "Hello"; // points to above "Hello"
// somewhere in your program, assume that pString is a
// line app read from a file - a new object
textbox->Text = pString; // textbox->Text points a new object

Open disassembly window (and registers) to see what's happening to ecx.

Ismail

Nov 17 '05 #2
ismailp wrote:
First of all, in unmanaged code, if you assign a constant string to a
char*:

const char* szText1 = "Hello";
const char* szText2 = "Hello";

compiler generates one "Hello" and both szText1 and szText2 point the
same location.

Actually it is not guaranteed that they point to the same location on this. A compiler is
free to optimise them to one string literal, however this is not guaranteed, and we should
not rely on this. If we want to make sure that they point to the same place, we must make
a pointer comparison.

Very same rule applies to managed applications as well.

What happens when you "type" inside the textbox deals with Windows.

Actually when we type in a .NET TextBox, our characters get added to the System::String
pointed by the Text property. My question was if a new String object gets created
implicitly, or in effect, we are on the string literal in this case.

After all, it is a Windows "EDIT" control. Read GetWindowText on MSDN
to learn more about how windows manage their window texts.

I assume this is some Win32 API function. However I do not know Win32 API.

So,

String* pString1 = "Hello";
String* pString2 = "Hello";

will point the same location - no new string is created for pString2.
However, a new String object is constructed, and these two pointers
will be pointing to that object.

If implicit String creation takes place, I do not think both of these will be pointing to
the same object, the same as the case
String *pString1= __gc new String("Hello");
String *pString2= __gc new String("Hello");
or the case of standard C++:
string s1= "Hello";

string s2= "Hello";
The above two string objects are not the same one.

(Write a sample, and open
disassembler, see "mov ecx,.." sections. ECX is "this" pointer.)

..NET has its own assembly language and .NET assembler/disassembler. And I do not know IL
assembly so far.
Nov 17 '05 #3
Ioannis Vranos wrote:
When we assign a managed or unmanaged string literal to a String *,
is a new String created implicitly in the managed heap?
A managed string literal IS a string on the managed heap. If you assing it
to a String^ (or __gc String* in MC++), you're just aliasing an existing
string.
Or in other words, what happens when we assign a string literal to
TextBox::Text and then writing in this TextBox?


Assigning to TextBox::Text will alias the managed string (make a new
reference to it), and also create an unmanaged copy that gets passed to the
edit control, which probably makes another copy of that. When you type in
the text box, the edit control is managing the contents of that native
string - I would expect it keeps a large enough buffer that it doesn't need
to re-allocate every time you type a character.

When you subsequently access the .Text property of the TextBox it will
re-fetch the text from the native textbox and make a new manged copy of it.

Remenber that managed strings are immutable - whenever something changees
the value of a string, it's really creating a new string and abandoning the
old one (which may yet be referenced by another handle).

-cd
Nov 17 '05 #4
Carl Daniel [VC++ MVP] wrote:
A managed string literal IS a string on the managed heap. If you assing it
to a String^ (or __gc String* in MC++), you're just aliasing an existing
string.

Just to mention here that under C++/CLI there is no special 'S' prefix to a string
literal, that is I think all string literals are considered managed, at least when
assigned to String ^, implicitly probably.

Remenber that managed strings are immutable - whenever something changees
the value of a string, it's really creating a new string and abandoning the
old one (which may yet be referenced by another handle).

Right. So if I get it right, for a TextBox named TextBox1, if we do:
TextBox1->Text= "Some text";
and then type in the text box, it will generate its own Strings as usual and will not
overwrite the literal.
Nov 17 '05 #5
exactly. TextBox1->Text will point a new string. As Carl mentioned, it
could be something like following code:

__property void set_Text(String* pStr)
{
const wchar_t __pin * lpszText = PtrToStringChars(pStr);
SetWindowTextW(m_hwnd, lpszText);
}

__property String* get_Text()
{
int nLen = GetWindowTextLength(m_hwnd);
LPWSTR lpszText = (LPWSTR)LocalAlloc(LPTR, sizeof(wchar_t) * (nLen +
1));
GetWindowTextW(m_hwnd, lpszText, nLen + 1);
String* pStr = new String(lpszText);
LocalFree(lpszText);
return pStr;
}

That allocates a new String* and return it.

When you debug your managed code with, you can see normal x86 assembly
instructions - not MSIL. I guess, JIT debugging happens during
MSIL-to-native conversion. So you can still see "ecx".

Ismail

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
8
by: Ioannis Vranos | last post by:
In .NET (and C++/CLI) there is an overloaded String == operator for handles. That is when we do comparison of two String handles the contents of the Strings are compared instead of their addresses....
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
24
by: Marcus Kwok | last post by:
Hello, I am working on cleaning up some code that I inherited and was wondering if there is anything wrong with my function. I am fairly proficient in standard C++ but I am pretty new to the .NET...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
2
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) {...
4
by: John Smith | last post by:
How can I allow someone to cast my C# class into System.String? Is it possible in C#? In C++ I used "operator" keyword to mark C++ member function.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.