Solved

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

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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