473,785 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unsafe?????????

private void button1_Click(o bject sender, EventArgs e)
{
unsafe
{
string str = "abcde";
fixed (char* pStr = str)
{
A a = new A(pStr);
a.change();
MessageBox.Show (str.ToString() );
}
}
}
}

public class A
{
unsafe char* pStr = (char*)0;
public unsafe A(char* sss)
{
pStr =sss;
}

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
Jun 17 '07 #1
2 1938
You might try spending a little more thought on your Subject: field for
your posts, so that it better describes the question you have, as well as
on the actual phrasing of your question in the post (for example, putting
more than just the code and "error,why" ).

Also, the Typographical Conservancy phoned, and they want to know why the
question mark population had a sudden decrease. I'll suggest that you can
help them avoid becoming endangered by not using so many, especially since
putting extra question marks in your post doesn't make the post any more
likely to be answered.

Now, all that said:

On Sun, 17 Jun 2007 09:47:39 -0700, wmhnq <wm***@126.comw rote:
public class A
{
unsafe char* pStr = (char*)0;

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
You are assigning a string reference to a character. You didn't post the
compiler error, but I suspect it says exactly that: something about cannot
convert a string to a character.

You might prefer this line of code:

pStr = "fghijkl";

That should work better.

Pete
Jun 17 '07 #2
On Jun 17, 11:47 am, "wmhnq" <w...@126.comwr ote:
private void button1_Click(o bject sender, EventArgs e)
{
unsafe
{
string str = "abcde";
fixed (char* pStr = str)
{
A a = new A(pStr);
a.change();
MessageBox.Show (str.ToString() );
}
}
}
}

public class A
{
unsafe char* pStr = (char*)0;
public unsafe A(char* sss)
{
pStr =sss;
}

public unsafe void change()
{
*pStr = "fghijkl";//error,why??????

}
}
I'm not understanding the need for unsafe code in your example.

Jun 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1916
by: Elidel | last post by:
Is the use of a COM object in a c# program considered 'unsafe' code ? Does the method that calls the COM object need the 'unsafe' keyword ? Is code that calls a COM object considered unmanaged? Is 'unsafe' code the same as unmanaged code ?
3
3150
by: Andre | last post by:
if I have an unsafe method that takes in two arrays, performs some operations and returns; I know that the garbage collector will not collect objects allocated inside the unsafe method. However, if I'm calling this method from a main() method of a class which is safe (i.e managed), will the allocated array (which, suppose, has been allocated inside the main() method) be collected by the garbage collector after it's been used? Also, how do...
8
5461
by: | last post by:
Wel, I am rebuilding the VC# 2002 project that I have deployment problems with the 2003 version, hoping this solves the problems, but now I encounter this wierd bug??? If I have the project, and do not compile with "Allow Unsafe Code Blocks=false" set to true, then the project compiles and no problems. BUT if I compile with "Allow Unsafe Code Blocks=true" then I het the error below. With unsafe {} of unsave{} removed it dos not solve the...
4
609
by: Jon Milner | last post by:
How do I declare that my code is unsafe? Sorry the help files at my University have not been installed!
17
1877
by: Bradley1234 | last post by:
Sorry if this is obvious, but Ill ask... Is there a new way of using pointer operations in C# ? Ive got a Deitel book on C# that neither mentions the word "pointer" nor "unsafe" in the index. coming from a C/C++ world, pointers make perfect sense, and getting up to speed on CS, did pointers go away?
4
4319
by: CodeTyro | last post by:
My native language being C++, I've got a few questions that a couple of hours of searching on msdn didn't answer. First, when using unsafe code and pointers, what is the C# equivalent to the C++ "delete" command? I would like to keep my memoryspace clean, but I've been unable to find a way to do it thus far. Second, I'm using structs to store a specific set of data. Is there a C# equivalent to the C++ STL "Vector" (or any of the...
1
1643
by: Z | last post by:
Hello, In my C# program I have a function that uses pointers. Is there a way to tell the compiler to compile only this function with the unsafe switch and compile the remaining project as safe code? I enclosed the body of the function in unsafe block like this: public static void Foo() { unsafe
2
3390
by: NickP | last post by:
Hi there, I am currently moving my API declarations into relevant classes for Safe and Unsafe methods. My understanding is that unsafe methods are ones that require elevated security priviledges in order to perform their function. Is there a reference available as to what methods are safe and which unsafe? For example I cam currently looking at the method SHGetFileInfo, I presume this is unsafe as you would need to have priviledges in...
0
1255
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I have two piece of unsafe code. In the first one I'm getting the byte* pointer of a Bitmap data, and the second one in inside a loop that uses that byte* pointer to set the data in the Bitmap. The trouble is that that each of this pieces of code are bound by a different unsafe section, therefore I'm getting an error that the second unsafe section does not "The name 'refBuf' does not exist in the current context" ...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.