Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Altering memory directly with C is easy; can it be done in C#?

Posted on 2014-02-28
3
Medium Priority
?
224 Views
Last Modified: 2014-04-04
I started thinking about how to make applications more secure, and a question popped into my head.  I've looked into working with unsafe pointers in C#, but mostly that just deals with the reading of information.

Given this piece of code:

string a = "one";
string b = a;
ChangeTheMiddleLetter(b, 'X');
MessageBox.Show(a);
ChangeTheMiddleLetter(b, 'n');
MessageBox.Show(a);


Could you code ChangeTheMiddleLetter such that the message box would show "oXe" the first time and "one" the second?

In other words, assuming that a and b both point to the same memory location, could you alter one character of what is being pointed to without altering the pointer address?  Or is that no longer possible with managed code?
0
Comment
Question by:FrancineTaylor
3 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 39897153
The same functionality can be done by c sharpstring methods . Why you want do using pointers?
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 39897228
Yes agreed with apeter

Unsafe string manipulation is inherently incorrect. .NET strings aren't supposed to be edited, and it's most likely that there is code in the framework that is built around the assumption that a string will never change. Anything that relies on String.GetHashCode() comes immediately to mind, but there might be behind-the-scenes optimizations or sanity checks. Presumably it's something like that can cause the CLR error.

If you're finding, after profiling, that .NET's immutable string implementation does not fit your needs, the easiest mutable alternative that would let you modify its length is a List<char>.
0
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 2000 total points
ID: 39897353
You are talking about using 'unsafe' code whi ch is frowned upon but sometimes cannot be avoided. In answer to your question, yes you can. :)

Take a look at http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx for some info.

Manipulating strings in this way might or might not work. This is due to the fact that strings are not fixed in memory but can change location runtime. A pointer therefor might point to something you did not anticipate.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Screencast - Getting to Know the Pipeline

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question