Solved

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

Posted on 2014-02-28
3
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

632 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