473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String Init

Sometime ago, i posted a concern about initializing string variables to null
v string.empty. I can't find the thread to reply to it so excuse this one.

I just now thought of this. Isn't it dangerous to initialize variables to
null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of
the routine i intialize an object to null. I intend to use this variable at
line 160 for example. At line 10, a garbage collection occurs. Is my code at
line 160 safe, the object is eligible for garbage collection because it has
no roots? This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given
it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would
prevent this situation from occuring?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Nov 15 '05 #1
8 1994
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
Sometime ago, i posted a concern about initializing string variables to null
v string.empty. I can't find the thread to reply to it so excuse this one.

I just now thought of this. Isn't it dangerous to initialize variables to
null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of
the routine i intialize an object to null. I intend to use this variable at
line 160 for example. At line 10, a garbage collection occurs. Is my code at
line 160 safe, the object is eligible for garbage collection because it has
no roots?
What object? There *is* no object, because you've set the variable to
null. Don't forget: the value of a reference-type variable isn't an
object, it's a reference. A reference is either a way of identifying an
object, or null.
This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given
it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would
prevent this situation from occuring?


No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
> No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)
well that is grasping to me. i will think about creating a small example
which should demonstrate that this may be a problem. Something real world,
aside from the obvious string example.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
Sometime ago, i posted a concern about initializing string variables to null v string.empty. I can't find the thread to reply to it so excuse this one.
I just now thought of this. Isn't it dangerous to initialize variables to null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of the routine i intialize an object to null. I intend to use this variable at line 160 for example. At line 10, a garbage collection occurs. Is my code at line 160 safe, the object is eligible for garbage collection because it has no roots?


What object? There *is* no object, because you've set the variable to
null. Don't forget: the value of a reference-type variable isn't an
object, it's a reference. A reference is either a way of identifying an
object, or null.
This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would prevent this situation from occuring?


No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
//managed c++public class Proof {
private int _value;

public Proof ( int v ) { _value = v; }

public static implicit operator int ( Proof c ) {
return c._value;
}
public static explicit operator double ( Proof c ) {
return (double)c._value;
}
}
//new objectProof* c = new Proof(15);//share allocation
int* c1 = *c;[snip]
*c = null;[garbage collection fires off right here]c1 = new Proof(20);Your
explanation for c1 at this point?
-- Regards,Alvin Bruney [ASP.NET MVP]Got tidbits? Get it
here...http://tinyurl.com/3he3b"Alvin Bruney [MVP]" <vapor at steaming post
office> wrote in message news:uI**************@TK2MSFTNGP11.phx.gbl...
No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)
well that is grasping to me. i will think about creating a small example
which should demonstrate that this may be a problem. Something real world,
aside from the obvious string example.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
Sometime ago, i posted a concern about initializing string variables
to null v string.empty. I can't find the thread to reply to it so excuse this one.
I just now thought of this. Isn't it dangerous to initialize variables to null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of the routine i intialize an object to null. I intend to use this
variable
at line 160 for example. At line 10, a garbage collection occurs. Is my code at line 160 safe, the object is eligible for garbage collection because
it
has no roots?


What object? There *is* no object, because you've set the variable to
null. Don't forget: the value of a reference-type variable isn't an
object, it's a reference. A reference is either a way of identifying an
object, or null.
This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would prevent this situation from occuring?


No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #4
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)
well that is grasping to me. i will think about creating a small
example which should demonstrate that this may be a problem. Something
real world, aside from the obvious string example.
In what way is it grasping? If you're not going to use an object,
there's no point in creating it. Setting the value of a variable to
null has no unwanted side-effects as far as I can see. (Setting it to a
reference to an "empty" object has the side-effect of requiring that
object, and the other one previously talked about of coding mistakes
not being spotted where they would throw a (justified) exception
otherwise.)
//managed c++public class Proof {
private int _value;

public Proof ( int v ) { _value = v; }

public static implicit operator int ( Proof c ) {
return c._value;
}
public static explicit operator double ( Proof c ) {
return (double)c._value;
}
}
//new objectProof* c = new Proof(15);//share allocation
int* c1 = *c;[snip]
*c = null;[garbage collection fires off right here]c1 = new Proof(20);Your
explanation for c1 at this point?


Could you give a C# example rather than a C++ example? I don't know the
semantics of MC++ well enough to comment, really. You also need to
explain what you believe the problem shown by your example is - why is
it showing a bad thing?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5
i'm going to drop this one. i don't think i have arguments that can hold up
in court to support this.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
No - because then you're probably creating an object for no reason.
(Not in the case of String.Empty, but in other cases.)
well that is grasping to me. i will think about creating a small
example which should demonstrate that this may be a problem. Something
real world, aside from the obvious string example.


In what way is it grasping? If you're not going to use an object,
there's no point in creating it. Setting the value of a variable to
null has no unwanted side-effects as far as I can see. (Setting it to a
reference to an "empty" object has the side-effect of requiring that
object, and the other one previously talked about of coding mistakes
not being spotted where they would throw a (justified) exception
otherwise.)
//managed c++public class Proof {
private int _value;

public Proof ( int v ) { _value = v; }

public static implicit operator int ( Proof c ) {
return c._value;
}
public static explicit operator double ( Proof c ) {
return (double)c._value;
}
}
//new objectProof* c = new Proof(15);//share allocation
int* c1 = *c;[snip]
*c = null;[garbage collection fires off right here]c1 = new

Proof(20);Your explanation for c1 at this point?


Could you give a C# example rather than a C++ example? I don't know the
semantics of MC++ well enough to comment, really. You also need to
explain what you believe the problem shown by your example is - why is
it showing a bad thing?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #6
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
i'm going to drop this one. i don't think i have arguments that can hold up
in court to support this.


There's no need for them to hold up in court for them to be interesting
- whether they're right or wrong, you may well find it helpful to talk
them through anyway, and I'm more than happy to oblige.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
I know this doesn't address your main concern, but if you're not going to
use your variable until line 160, why not leave declaring it until line 159?
ie;

instead of:

void Foo()
{
Bar bar = null;

// 150 lines of stuff

bar = new Bar();

// etc
}

just do:

void Foo()
{
// 150 lines of stuff

Bar bar = new Bar();

// etc
}

I know we used to have to do it in C but there's no need in C#.
"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:uM*************@tk2msftngp13.phx.gbl...
Sometime ago, i posted a concern about initializing string variables to null v string.empty. I can't find the thread to reply to it so excuse this one.

I just now thought of this. Isn't it dangerous to initialize variables to
null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of
the routine i intialize an object to null. I intend to use this variable at line 160 for example. At line 10, a garbage collection occurs. Is my code at line 160 safe, the object is eligible for garbage collection because it has no roots? This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't given it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which would prevent this situation from occuring?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b

Nov 15 '05 #8
agreed

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
"Stu Smith" <st*****@remove.digita.com> wrote in message
news:ep**************@TK2MSFTNGP10.phx.gbl...
I know this doesn't address your main concern, but if you're not going to
use your variable until line 160, why not leave declaring it until line 159? ie;

instead of:

void Foo()
{
Bar bar = null;

// 150 lines of stuff

bar = new Bar();

// etc
}

just do:

void Foo()
{
// 150 lines of stuff

Bar bar = new Bar();

// etc
}

I know we used to have to do it in C but there's no need in C#.
"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
news:uM*************@tk2msftngp13.phx.gbl...
Sometime ago, i posted a concern about initializing string variables to null
v string.empty. I can't find the thread to reply to it so excuse this one.
I just now thought of this. Isn't it dangerous to initialize variables to null in a garbage collected environment since garbage collection is
non-dert...yada yada yada.

Consider this, i have a routine spanning 200 lines of code. at the top of the routine i intialize an object to null. I intend to use this variable

at
line 160 for example. At line 10, a garbage collection occurs. Is my

code at
line 160 safe, the object is eligible for garbage collection because it

has
no roots? This is theoretical and not apt to occur frequently but, right
now, i'm thinking this is terrible if it can actually occur. (haven't

given
it much thought just yet)

wouldn't it be best to intialize an object to a root like empty which

would
prevent this situation from occuring?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b


Nov 15 '05 #9

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

Similar topics

5
by: gtz669 | last post by:
I have a class which I am using for data stroage. I declare an instance of that class in my main class which is running my java applet. I Iassign it a value in the init () function and it works...
0
by: Doug Farrell | last post by:
Hi everyone, I'm trying to build a program to interface to a SOAP/XML interface provided by one of our vendors. This interface is built with MS SOAP Toolkit 3.0, so I'm guessig they are running...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
0
by: Old Wolf | last post by:
The purpose of the following code is to search a string for a substring, and replace the first character of the found string with a new value: #include <algorithm> #include <string> ...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
7
by: puzzlecracker | last post by:
Why would this work? - and it does! any thoughts? #include<stdio.h> void init(char **str){ *str="Awesome"; } main(){
1
by: mato | last post by:
this is what i experienced in my web application and i realy do not understand. this is my web page class behind public class MyWebPage : System.Web.UI.Page { //some page controls ..... ...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
2
by: bevis | last post by:
I'm new to sql server and mysql but this seems like it should be a pretty straight forward jdbc connection. But I have spent almost 2 days just trying to get a jdbc connection. Please help if you...
5
by: surapong | last post by:
Is there any easy way to extract the string from enum variable?? e.g. I have enum TheEnum {ONE, TWO, THREE}; I would like to generate the array of string containing {"ONE", "TWO", "THREE"}
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
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
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...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.