C # pointer operations

Source: Internet
Author: User

 

I heard people say that C # has no pointer, because it is not safe. Baidu just said this:




Now, you need to operate the hardware watchdog to know that C # can also operate the pointer.

Program environment:. Net microframework
Development tools: Visual Studio 2013
Development language: C #
Communication tool: New Life CAPTCHA Tool

Program function: our development hardware may crash in special peripheral circumstances. The solution is to use the watchdog program to discover the dead and then C # reset the MCU pointer. Restart the MCU.


C # single-chip microcomputer pointer
The procedure is as follows:




Code:

 

Using system; using Microsoft. spot; using system. threading; namespace doiterror {// <summary> // select generate in project properties /// select allow unsafe code in generate /// Mark unsafe modifier in class to allow use Pointer /// </Summary> public class program {// <summary> to kill hardware </Summary> Public static void main () {int A = 0; while (true) {A ++; thread. sleep (1000); debug. print (. tostring (); If (A = 20) {debug. print ("---------- execute ------------"); New testclass (). todo (); Return ;}}public unsafe class testclass {public void todo () {// declare a pointer int * I; // assign a value to the pointer, point to a nonexistent memory address I = (int *) 0x00000000; // obtain the pointer var J = * I ;}}}}

 

The above code implementation function: Count, 20 seconds later, the microcontroller crashes, restart continues to start.

 

Of course, C # supports pointer operator operations, just to ensure the security of Vs, We need to enable the pointer function;

 

Solution

1. Set "attribute"> "generate" in the vs project and select "allow Insecure code"

2. Add the unsafe modifier to the class where the MF program uses pointers.

3. Declare a pointer int * I; assign a non-existent memory address for I = (int *) 0x00000000; Value: var J = * I;




In this way, vs supports pointer compilation.

Then the hardware is output.

Download routine: csharp .rar


It's a cool day, and it's hard to work on code during the night. Send a beautiful girl to reduce the temperature.

 

 

Welcome to join us,Share the inspirational story of programmers.Happy programmers QQ group:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.