Two-dimensional array in memory [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Array memory allocation - paging
Does byte[,] myArray is always in continuous memory block?
I have some code which works with images stored in such arrays, everything works fine, but I just ask for sure.
EDIT: I work with unsafe code.

Yes, it's always there, in memory and will be memorized consecutively.
take a look : True Unsafe Code Performance
And msdn says you can use without think about it: http://msdn.microsoft.com/en-us/library/28k1s2k6%28v=vs.80%29.aspx

Related

Is there any existing function to get MAX length of String type in C# library [duplicate]

This question already has answers here:
What is the maximum possible length of a .NET string?
(8 answers)
Closed 4 years ago.
I know in C# you can do int.MaxValue or long.MaxValue to get the maximum for these two types, I was wondering if there are similar ways to get the maximum length for the string.
I don't see it here.
Also, I am aware that there are already questions being asked on max string length. Max string length in C#
I am asking if there is an existing function that comes in handy where you can use it to set the condition when and when not to truncate your string to avoid program crashing.
Or what is the normal approach?
E.g. Maybe something as easy as Max(string.length)?
No. In the same way that there is no known method to find the max length of an array you can allocate. Until you try to allocate the memory, you can't know if there is enough virtual address space and enough physical memory to contain it. Theorically you could VirtualAlloc (Windows API) greater and greater memory blocks until it fails, deallocate the block and then try to allocate the same amount of memory in .NET, knowing that the memory is there so the .NET should be able to allocate it.
Note that this is true at 32 bits... I haven't ever seen an out-of-memory error at 64 bits.

C# Allocate array to stack? [duplicate]

This question already has an answer here:
How to allocate arrays on the stack for performance gains?
(1 answer)
Closed 7 years ago.
I am writing a program in C#. Everything centers around a static 2D int array which is 400x6 elements. Just a few values will be updated just once every minute. But after each minute's updates, dozens of functions will read the values millions of times to compute "pattern scores". The faster the calculations, the more distinct functions I can cram in there. Realistically I can allow 30 seconds for this scoring process. Is there a way to allocate the static array to the stack, and if so, would this help the speed? Thanks.
Yes, you can alloc arrays on the stack in C# using "stackalloc" in "unsafe mode", but benchmarks shows a limited performance gain and the risk is that you hit the 1Mb stack size limit... which will give you a... StackOverflow(tm)!
Here is a good article on the subject:
http://blogs.microsoft.co.il/sasha/2013/10/17/on-stackalloc-performance-and-the-large-object-heap/
You can use "stackalloc" to alloc array directly on the stack.
Some documentation about :
https://msdn.microsoft.com/en-us/library/aa664785(v=vs.71).aspx
You can also use an implementation of Hamming weight which is describe here :
How to allocate arrays on the stack for performance gains?

C#, multithreaded filling of a multidimensional array [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Are C# arrays thread safe?
I have a program that is single-threaded at the moment, which basically puts lots of computed data into a multidimensional array (e.g. double[,] ; string[,] ).
Is it possible to assign segments of this array to different threads ? More precisely, if I make sure only one thread will write at a given coordinate, will there be some lock mechanism triggered ?
In terms of concurrency problems, you will be fine as long as your threads do not read or write to the same portion of your array concurrently. You may see slowdowns because of "False Sharing" hazard, though, so you may want to be on the lookout for unexpected slow-downs when the number of threads increases.
if I make sure only one thread will write at a given coordinate
Then you are safe. Assuming you don't resize the array etc.
If you are now using a for loop you can probably simply switch to Parallel.For(0, n, method)

Why does .NET save simple types (int,..) in stack? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why value-types are stored onto Stacks?
I understand about boxing / unboxing but my question is why .NET make the choice of store ie. an integer in stack ?
Its logical when you are (simple) passing parameters between procs / functions... but... why...is this generallly used in .NET in any case ?
Thanks a lot!
Not all integers are in stack.only integers which are local to functions or passed between functions are stored in stack.
If the integer is a part of class(ref type) then the integer is stored on the place where the ref types are stored(managed heap)
Because allocating heap objects for every integer and dereferencing the pointers all the time would be orders of magnitude too slow.

SortedList<K,V> vs SortedDictionary<K,V> vs Dictionary<K,V> [duplicate]

This question already has answers here:
SortedList<>, SortedDictionary<> and Dictionary<>
(6 answers)
Closed 9 years ago.
I have a large collection of small objects, each has a unique string ident. I need to decide which class to use.
MSDN says about the first two
The two classes have similar object
models, and both have O(log n)
retrieval. Where the two classes
differ is in memory use and speed of
insertion and removal
Since I rarely insert, mostly just retrieve it seems both are good for me. What about the plain old Dictionary?
Plain-old dictionary is the best option if you're not interested in sorting (since it's O(1) retrieval). If you're not going to modify the list much you should use SortedList since it uses less memory.

Categories