MyMutableString Class

V

vaib

Hi all,

I am relatively new to .net C# and I have the following query :

What should I do if I have to make a class , say, MyMutableString ,
that takes in a String object ( any string literal ) and does
modifications ( like append, etc ) on the same memory location where
that String object is stored without making use of pointers (, ie, not
making use of any unsafe code ). So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ?? If I am able to do it then
making the "MyMutableString" class won't be a hassle.

I have searched a lot for this problem and no solution seems to be in
sight. So i turn to usenet ( as always ) for help.

Thanking in anticipation.


Regards,
Vaibhav
 
A

Alberto Poblacion

vaib said:
What should I do if I have to make a class , say, MyMutableString ,
that takes in a String object ( any string literal ) and does
modifications ( like append, etc ) on the same memory location where
that String object is stored without making use of pointers (, ie, not
making use of any unsafe code ). So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ?? If I am able to do it then
making the "MyMutableString" class won't be a hassle.

I have searched a lot for this problem and no solution seems to be in
sight. So i turn to usenet ( as always ) for help.

Inside your class, you could allocate a char array with enough space for
the largest string that you wish to manipulate. Then store the characters of
the string in the char array. You can manipulate the string without
allocating or deallocating memory by just moving around the chars in the
array (and keeping a local variable to serve as an index to the last
position in use).
 
P

Peter Duniho

[...] So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ??

Um, the same way you access any memory location without the use of unsafe
code: use the appropriate variables. I.e. a variable that references an
object where the data is stored.

That said, if what you want is a mutable string, why not just use the
StringBuilder class?
 
G

Göran Andersson

vaib said:
Hi all,

I am relatively new to .net C# and I have the following query :

What should I do if I have to make a class , say, MyMutableString ,
that takes in a String object ( any string literal ) and does
modifications ( like append, etc ) on the same memory location where
that String object is stored without making use of pointers (, ie, not
making use of any unsafe code ). So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ?? If I am able to do it then
making the "MyMutableString" class won't be a hassle.

I have searched a lot for this problem and no solution seems to be in
sight. So i turn to usenet ( as always ) for help.

Thanking in anticipation.


Regards,
Vaibhav

You can't change the contents of a string without using unsafe code, and
you should never change the contents of a string anyhow. Everything in
the framework that uses strings rely on the fact that strings are
immutable, so if you change the contents of a string it can break pretty
much anything.

There are some alternatives that may do what you want:

You can use a StringBuilder, which is mutable and can produce a regular
string with the ToString method. (Note that the ToString method will
return the internal buffer as the string, so if you continue to use the
StringBuilder it will create a new buffer and copy all the contents to
it so that it can change it without changing the returned string.)

You can simply put a regular string in the class. Your class will be
mutable, but not the string that it contains.
 
V

vaib

   Inside your class, you could allocate a char array with enough space for
the largest string that you wish to manipulate. Then store the charactersof
the string in the char array. You can manipulate the string without
allocating or deallocating memory by just moving around the chars in the
array (and keeping a local variable to serve as an index to the last
position in use).

but character array would not be at the same memory location. I need
to access the memory location of the string..!
 
V

vaib

[...] So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ??

Um, the same way you access any memory location without the use of unsafe 
code: use the appropriate variables.  I.e. a variable that references an  
object where the data is stored.

That said, if what you want is a mutable string, why not just use the  
StringBuilder class?

but the reference would not allow me to manipulate the String
object..!
 
A

Alberto Poblacion

vaib said:
but character array would not be at the same memory location. I need
to access the memory location of the string..!

This is a very bad idea. Even if you were able to find the location, the
Framework and the compiler assume that the strings are immutable, and many
things can go wrong if you modify them.
For instance, the compiler can store a single string and use two
references to that location if the source code assigns the same constant to
both strings. If you access the location directly and modify the contents,
you may be changing something in another part of the program, which was
accessing the same location which was allowable because it was supposed to
be immutable.

Anyway, you said you wanted to do that without using unsafe code, which I
am pretty sure is not going to be possible. You are going to need either
unsafe or unmanaged code.

Therefore, if you want to create a class for "MyMutableString", you
should store the string contents on an alternative storage (such as a
char[]), not inside a real System.String.
Of course, this has already been done. It is called "StringBuilder".
 
J

J.B. Moreno

vaib said:
but character array would not be at the same memory location. I need
to access the memory location of the string..!

An unchanging memory location and a mutable string are physically
incompatible, unless you restrict the maximum size of the string to
something fairly small or are only planning on having one mutable
string.

As is using any managed resource.
 
P

Peter Duniho

but the reference would not allow me to manipulate the String
object..!

Within the context of .NET, it is impossible to literally "manipulate the
String object" and still have valid, testable code. Unfortunately, your
question is so vague that it's practically impossible to offer any
specific, useful advice. But the fact is, if you simply change your code
so that it uses StringBuilder where you want a "mutable string", it will
work.

Note that this will include changing any consumer of the string. But
that's not a problem at all, because you absolutely should not be changing
an actual String instance that is in use by code that isn't already
written and compiled with the knowledget that the String instance could
change; and if you're writing and compiling the code with the knowledge
that the String instance can change, you can just as easily make the type
StringBuilder instead of String.

Bottom line: use StringBuilder.

Pete
 
H

Harlan Messinger

vaib said:
Hi all,

I am relatively new to .net C# and I have the following query :

What should I do if I have to make a class , say, MyMutableString ,
that takes in a String object ( any string literal ) and does
modifications ( like append, etc ) on the same memory location where
that String object is stored without making use of pointers (, ie, not
making use of any unsafe code ). So the question boils down to - How
can I possibly access the memory location where the String object is
stored without the use of unsafe code ??

By definition, code in the managed code environment that accesses
anonymous locations in memory is "unsafe code", and a thing that points
to a location in memory is a pointer. So your question is
self-contradictory. (Yes, you could be asking for an API method that
encapsulates pointers rather than having you access them directly, but
clearly you already know that such a method doesn't exist because
otherwise you would use that.)
If I am able to do it then
making the "MyMutableString" class won't be a hassle.

There's a reason why System.String is a sealed class. For the sake of an
application's integrity, you aren't supposed to have access to or alter
*any* of its implementation.

Since the implementation of C# strings is hidden, you can't even make a
reliable supposition, as you are making, that they are implemented as a
single block of characters!

Since character arrays or StringBuilders would allow you this kind of
access yet you seem not to find them acceptable as solutions, it seems
that you are really determined to break the intentional encapsulation of
the String object. If that isn't correct, perhaps if you explained your
application-level goal it would help in the search for a solution.
 
V

vaib

By definition, code in the managed code environment that accesses
anonymous locations in memory is "unsafe code", and a thing that points
to a location in memory is a pointer. So your question is
self-contradictory. (Yes,  you could be asking for an API method that
encapsulates pointers rather than having you access them directly, but
clearly you already know that such a method doesn't exist because
otherwise you would use that.)


There's a reason why System.String is a sealed class. For the sake of an
application's integrity, you aren't supposed to have access to or alter
*any* of its implementation.

Since the implementation of C# strings is hidden, you can't even make a
reliable supposition, as you are making, that they are implemented as a
single block of characters!

Since character arrays or StringBuilders would allow you this kind of
access yet you seem not to find them acceptable as solutions, it seems
that you are really determined to break the intentional encapsulation of
the String object. If that isn't correct, perhaps if you explained your
application-level goal it would help in the search for a solution.

thanx to all for the answers....i think its not possible to do it with
the constraints i've put.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top