Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7046089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:36:32+00:00 2026-05-28T02:36:32+00:00

I use next unsafe code for string modifying: public static unsafe void RemoveLastOne(ref string

  • 0

I use next unsafe code for string modifying:

public static unsafe void RemoveLastOne(ref string Str1)
    {
        if (Str1.Length < 1)
            return;

        int len = Str1.Length - 1;
        fixed (char* pCh1 = Str1)
        {
            int* pChi1 = (int*)pCh1;
            pCh1[len] = '\0';
            pChi1[-1] = len;
        }
    }

But some time later my C# programm crash with exception:

FatalExecutionEngineError:
“The runtime has encountered a fatal error.
The address of the error was at 0x6e9a80d9, on thread 0xcfc. The error
code is 0xc0000005. This error may be a bug in the CLR or in the
unsafe or non-verifiable portions of user code. Common sources of this
bug include user marshaling errors for COM-interop or PInvoke, which
may corrupt the stack.”

If I change function “RemoveLastOne” to “Str1 = Str1.Remove(Str1.Length – 1);” program works fine.

Why exception happens? And how I can implement unsafe change string in C# correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:36:33+00:00Added an answer on May 28, 2026 at 2:36 am

    String values in .Net are intended to be immutable. In this function you are taking an immutable value, mutating it in several visible ways (content and length) not to mention writing data before the original. I’m not surprised at all that this would result in a later CLR crash as it special cases String values in several places and writing before the pointer is simply dangerous.

    I can’t really see a reason why you’d want to do the unsafe manipulation here. The safe code is straight forward and won’t cause these types of hard to track down bugs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use next code to print current time Calendar cal = Calendar.getInstance(); System.out.println(cal.getTime()); I
I am use next type of strings: LPCSTR, TCHAR, String i want to convert:
In Flash Builder (flex 4) I try to use next code to set selected
I allocate a color entry with the next code, then I use it to
I use next code to know when a files is changed in a certain
I need property name in my app and I use next code to get
I use the next code from Google for Analytics: <script type='text/javascript'> var _gaq =
public synchronized X getAnotherX(){ if(iterator.hasNext()){ X b = iterator.next(); String name = b.getInputFileName(); ...
I use next code. [UIView transitionFromView:viewA toView:viewB duration:1.0 options: UIViewAnimationOptionTransitionFlipFromLeft completion: ^(BOOL inFinished) {
I use the next code to retrieve data from a table in the database:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.