|
Peter R. Fletcher wrote: a lot of copying will be necessary to (in effect) pass each struct item by value.
And that makes me think it is the reason why C# does not accept any changes to it: they probably don't copy but use the real stuff, which is fine if they disallow any code to change it.
|
|
|
|
|
Actually, they do make a copy, even in the optimized code.
In fact, two copies are made, at least in the code I tested.
See here:
shl rcx,4
mov eax,dword ptr [rbx+rcx+10h]
mov dword ptr [rsp+20h],eax
mov eax,dword ptr [rbx+rcx+14h]
mov dword ptr [rsp+24h],eax
mov eax,dword ptr [rbx+rcx+18h]
mov dword ptr [rsp+28h],eax
mov eax,dword ptr [rbx+rcx+1Ch]
mov dword ptr [rsp+2Ch],eax
lea rcx,[rsp+20h]
mov rax,qword ptr [rcx]
mov qword ptr [rsp+40h],rax
mov rax,qword ptr [rcx+8]
mov qword ptr [rsp+48h],rax
lea rax,[rsp+40h]
movss xmm5,dword ptr [rax]
The C# code:
static void Normalize(Float3[] array)
{
Float3 x = new Float3();
foreach (Float3 f in array)
{
x = f.Normalize();
}
if (array.Length != 0)
throw new Exception(x.ToString());
}
edit: made a slight mistake here, whatever.
If you change "the foreach value" (indirectly, of course), the second copy is affected but not the first. The first copy is never used again.
For primitive types, the code is actually sane.
modified on Monday, September 13, 2010 5:39 PM
|
|
|
|
|
You're sure that is a release situation? doesn't look very good then.
|
|
|
|
|
Release without debugger attached, then attach it later, I know
And yes, it's quite horrible..
edit: for primitive types they just read the value from the array and use it
|
|
|
|
|
harold aptroot wrote: for primitive types they just read the value from the array and use it
OK, one more reason to tip the class/struct balance in favor of struct then.
BTW: great info!
|
|
|
|
|
Thanks
By the way, they don't disallow all code to change "the foreach value". For example, if you have an array of Rectangle s you can still call Inflate on them. Code like that has the effect of changing the second copy, and that's the same as changing "the foreach value".
This snippet shows that behaviour:
Rectangle[] aBunchOfRectangles = new Rectangle[10];
foreach (Rectangle rect in aBunchOfRectangles)
{
Console.WriteLine(rect);
rect.Inflate(1, 1);
Console.WriteLine(rect);
}
That would explain why the struct is copied from the array to the stack once (though it is also done when the method doesn't change the instance), the other copy is still a mystery to me..
|
|
|
|
|
harold aptroot wrote: if you have an array of Rectangles you can still call Inflate on them
Yeah, Rectangle is a mutable struct! I assume they didn't make it immutable for some backward compatibility reason. I had never considered the implications of this (apart from the normal) until seeing your previous post - interesting
|
|
|
|
|
Hi
I'm a newbie programming .NET and need some help.
I have a windows form project in which I have instantiated a trackbar_scroll component (a horizontal slider). I'd like it to stay in the lower right corner of the window when resizing but I can't make it
What should I code in this method:
private void trackBar1_Scroll(object sender, EventArgs e)
{
}
Can someone please tell me how to do this?
Cheers
Tom
|
|
|
|
|
|
Yeah that did it!
Thanks...hmmmm, I have a looooong way to go!
Thanks
Tom
|
|
|
|
|
hi friends,
i want to insert value in a particular column of a row(datagridview) from a text box.
please help!
|
|
|
|
|
Sorry, but your question is not clear enough. Do you mean put the text from text box into grid?
In that case you can just access that particular column of the datagridview and then set it.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
sorry for late response
Basically i have a button and i try to insert value in datagridview on button click.Please help me
|
|
|
|
|
I was curious, I work for a PC repair shop, and we use alot of applications to get certain jobs done, but I also have a certain amount of applications that I use for OS Support, is there any way to deploy applcations we use using the Deploy and Package wizard to install it on out customers computers, for example, adding Malwarebytes dotnet4 and java offline installer in a deployment package?
|
|
|
|
|
Have a look here[^]. This link describes how to use custom file types while creating a setup.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
http://ninite.com/[^]
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my Blog
|
|
|
|
|
Is that what your customers want?
|
|
|
|
|
Hi,
I am trying to make a kind of desktop widgets using C#. So far, I've managed to create a window that gets added to the WorkerW window containing SHELLDLL_DefView. This way, my main form is inserted inside the desktop window over the wallpaper. This is exactly what I want except that I have a little problem. It seems like there is an opacity of 50% and a black is rendered as transparent. I don't know where this comes from and I don't know if I can change it. Does anybody knows what might be causing this and how can I fix it?
The code I used to do this is pretty simple. I find the handle to the WorkerW window that contains the SHELLDLL_DefView window as a child and then I call those functions to insert my own form inside as a child :
SetParent(this.Handle, hWndParent);
SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));
Here's how it looks when the form is in the desktop
Thank you
|
|
|
|
|
If you are trying to create a desktop widget then do some research with the Sidebar Gadget projects. The way you are doing it could be problematic.
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my Blog
|
|
|
|
|
After creating a VB.NET app in .NET Framework 4 I noticed that when I try using the application on several levels of windows whether it be XP/Vista or 7 it keeps throwing an error bout installing the suggested framework on the computer. How do I compile it so it is compatible with several OS's without the user having to install the .NET Framework in order for it to run?
|
|
|
|
|
Sorry to tell you, but if you build an application in dotNET 4.0, any computer that will attempt to execute the application will be required to have dotNET 4.0 installed, there is no way around that as the dotNET 4.0 assemblies provide the "under the hood" functionality that your application utilizes. About the only way to be a little more backwards compatible would be to target your project(s) to dotNET 2.0 and refactor any code that requires it to run in that mode.
|
|
|
|
|
.Net 4 apps require .Net 4
However you could add the .Net 4 prerequisite to you installer package to ensure it gets installed before your app runs.
|
|
|
|
|
If you don't need the latest features, you could target say 2.0 AND somehow tell your code will also run happily on higher .NET versions (for the rare situation a system has say 4.0 and no 2.0). I don't know the details, uncle Google knows for sure.
ADDED
Whatever targets 2.0 also runs fine on 3.0 and 3.5, as these are functional and technical supersets of 2.0
4.0 is a completely different thing; it is a functional superset too, but consists of all new files
1.0 and 1.1 should be considered obselete IMO.
/ADDED
|
|
|
|
|
Herboren wrote: with several OS's without the user having to install the .NET Framework in order for it to run
You need the right framework installed.
Or you could use multitargeting to compile your program in another version of .Net (provided you are not using features specific to .Net 4). But then you would still need that framework installed.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Target the runtime version which you think would be installed on all your target client OS.
|
|
|
|