theory

Why is cloning (in .NET) so difficult?

谁说我不能喝 提交于 2019-11-28 09:43:20
In the past I had the need to clone objects, only to find that they don't implement a Clone() method, forcing me to do it by hand (create a new instance and copy all properties from the original to the new one) Why isn't cloning as easy as duplicating the memory block the object is allocated in, and thus have the Clone method in the object class, having all classes in .NET inherit it? Because that wouldn't perform a deep clone, which is usually what clones really need to be. Imagine you have a reference to an array, or a list... simply copying the memory taken by your object will simply clone

What does “double free” mean?

那年仲夏 提交于 2019-11-28 09:42:40
As the title suggests I am new to C and have a mid-term coming up shortly. I am revising from past papers currently and a recurring theme is double free problem. I understand that it is the process of calling free() on the same memory location twice, but I have a couple of questions that I'm not 100% sure how to answer: Question 1: What is the result of a double free in C, and why is it such a problem? This will cause a double free: char* ptr = malloc(sizeof(char)); *ptr = 'a'; free(ptr); free(ptr); My response to this would be, would it return a 0x0 memory address and cause a system

Algorithm to print out a shuffled list, in-place and with O(1) memory

╄→гoц情女王★ 提交于 2019-11-28 08:32:38
After reading this question I started to wonder: is it possible to have a shuffling algorithm which does not modify or copy the original list? To make it clear: Imagine you are given a list of objects. The list size can be arbitrary, but assume it's pretty large (say, 10,000,000 items). You need to print out the items of the list in random order, and you need to do it as fast as possible. However, you should not: Copy the original list, because it's very large and copying would waste a LOT of memory (probably hitting the limits of available RAM); Modify the original list, because it's sorted

How can a HashSet offer constant time add operation?

假如想象 提交于 2019-11-28 07:30:02
问题 I was reading the javadocs on HashSet when I came across the interesting statement: This class offers constant time performance for the basic operations (add, remove, contains and size) This confuses me greatly, as I don't understand how one could possibly get constant time, O(1), performance for a comparison operation. Here are my thoughts: If this is true, then no matter how much data I'm dumping into my HashSet, I will be able to access any element in constant time. That is, if I put 1

What are good use cases for JavaScript self executing anonymous functions? [closed]

守給你的承諾、 提交于 2019-11-28 05:43:15
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I understand what a self executing anonymous function is but I am having trouble understanding where I would use them and why. This could be because I often use jQuery. Could you please provide examples of good use cases? 回答1: Basically, a self-executing anonymous function (more

Difference between convergence and idempotence in Chef

筅森魡賤 提交于 2019-11-28 05:28:05
What is the basic difference between convergence and idempotence in Chef? Convergence and idempotence are not Chef-specific. They're generally attributed to configuration management theory, though have use in other fields, notably mathematics. Let's start with the more basic, idempotent. We're going to ignore the mathematic use of idempotent, and focus instead on what configuration management people mean when they talk about it. That is: "multiple applications of the same action do not have side effects on the system state." A simple example of an idempotent operation is mkdir -p : mkdir -p

How to determine whether a language is LL(1) LR(0) SLR(1)

放肆的年华 提交于 2019-11-28 05:23:35
Is there a simple way to determine whether a grammar is LL(1), LR(0), SLR(1)... just from looking on the grammar without doing any complex analysis? For instance: To decide whether a BNF Grammar is LL(1) you have to calculate First and Follow sets - which can be time consuming in some cases. Has anybody got an idea how to do this faster? Any help would really be appreciated! First off, a bit of pedantry. You cannot determine whether a language is LL(1) from inspecting a grammar for it, you can only make statements about the grammar itself. It is perfectly possible to write non-LL(1) grammars

Purpose of singletons in programming

时光总嘲笑我的痴心妄想 提交于 2019-11-28 04:38:19
This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever created. This sounds a lot like a static class to me. The main difference being that with a static class you don't / can't instance it, you just use it such as Math.pi() . With a singleton class, you would still need to do something like singleton firstSingleton = new singleton(); firstSingleton.set_name("foo"); singleton secondSingleton = new singleton(); Correct me if i am wrong, but firstSingleton == secondSingleton right

Using IS NULL or IS NOT NULL on join conditions - Theory question

夙愿已清 提交于 2019-11-28 04:07:36
Theory question here: Why does specifying table.field IS NULL or table.field IS NOT NULL not work on a join condition (left or right join for instance) but only in the where condition? Non working Example: -this should return all shipments with any returns (non null values) filtered out. However, this returns all shipments regardless if anything meets the [r.id is null] statement. SELECT * FROM shipments s LEFT OUTER JOIN returns r ON s.id = r.id AND r.id is null WHERE s.day >= CURDATE() - INTERVAL 10 DAY Working example: -This returns the correct amount of rows which is total shipments, less

Which is the fastest way to get the absolute value of a number

若如初见. 提交于 2019-11-28 03:56:19
Which is the fastest way to implement an operation that returns the absolute value of a number? x=root(x²) or if !isPositive(x): x=x*(-1) Actually this question can be translated as, how fast is an if (and why please). My college programing professors always told me to avoid if s for they are extremely slow, but I always forgot to ask how slow and why. Does anybody here know? kquinn Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as calculating the square root. Rules of thumb from my assembly days: Integer or bitwise op: 1 cycle Floating