473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1666
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 = PtrToStringChar s(pStr);
SetWindowTextW( m_hwnd, lpszText);
}

__property String* get_Text()
{
int nLen = GetWindowTextLe ngth(m_hwnd);
LPWSTR lpszText = (LPWSTR)LocalAl loc(LPTR, sizeof(wchar_t) * (nLen +
1));
GetWindowTextW( m_hwnd, lpszText, nLen + 1);
String* pStr = new String(lpszText );
LocalFree(lpszT ext);
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
51740
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 have some legcay C code that needs to process a string taken from a textbox, then I need to re-display the string as the textbox->Text. I easily found how to convert from system::string to char but I can't figure out how to go the other way!!
15
10838
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
2312
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. However how can we do a handle comparison when we want to determine if we have to do with the same object? In the style
3
13014
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'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
1
18214
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 is serialized correctly, and the server returns a response (I've captured the response and it's correct!) but when the .NET deserialize this response, it throws the exception "System.InvalidOperationException: There is an error in XML
24
17492
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 managed C++. It seems to work fine, but everyone knows that programs with errors can still appear to "work fine" :) I am working with VS .NET 2003; I am unable to upgrade to 2005 at this time, so I cannot use the newer syntax or features. ...
2
5044
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 because in further editions of .net this will be deprecated or so on.. . so, please, anybody can tellme how to convert from System::String::^ to char* and viceversa?
2
13598
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) { this.Url = url;
4
3884
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
10643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3862
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.