Solved

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

Posted on 2014-02-28
3
205 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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now