theory

What does “double free” mean?

邮差的信 提交于 2019-12-17 19:17:42
问题 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);

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

旧城冷巷雨未停 提交于 2019-12-17 17:32:27
问题 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? 回答1: Conditionals are slower than plain arithmetic operations, but much, much faster than something as silly as

Bootstrapping a compiler: why?

两盒软妹~` 提交于 2019-12-17 15:37:31
问题 I understand how a language can bootstrap itself, but I haven't been able to find much reference on why you should consider bootstrapping. The intuitive answer is that the language you're writing offers utilities that are not found in the "base" language of the compiler, and the language's features are relatively well-suited for a compiler. For instance, it would make sense to bootstrap a C++ compiler -- it could potentially be much easier to maintain the compiler when OOP is properly used,

Why can't a recursive-descent parser handle left recursion

给你一囗甜甜゛ 提交于 2019-12-17 15:36:27
问题 Could someone please explain to me why recursive-descent parsers can't work with a grammar containing left recursion? 回答1: consider: A ::= A B the equivalent code is boolean A() { if (A()) { return B(); } return false; } see the infinite recursion? 回答2: For whoever is interested A ::= A B | A C | D | E can be rewritten as: A ::= (D | E) (B | C)* The general form of the transformation is: any one of the non left recursive disjuncts followed by any number of the left recursive disjuncts without

What is an NP-complete in computer science?

百般思念 提交于 2019-12-17 15:05:25
问题 What is an NP-complete problem? Why is it such an important topic in computer science? 回答1: NP stands for Non-deterministic Polynomial time. This means that the problem can be solved in Polynomial time using a Non-deterministic Turing machine (like a regular Turing machine but also including a non-deterministic "choice" function). Basically, a solution has to be testable in poly time. If that's the case, and a known NP problem can be solved using the given problem with modified input (an NP

How do I check if a directed graph is acyclic?

流过昼夜 提交于 2019-12-17 08:04:33
问题 How do I check if a directed graph is acyclic? And how is the algorithm called? I would appreciate a reference. 回答1: I would try to sort the graph topologically, and if you can't, then it has cycles. 回答2: Doing a simple depth-first-search is not good enough to find a cycle. It is possible to visit a node multiple times in a DFS without a cycle existing. Depending on where you start, you also might not visit the entire graph. You can check for cycles in a connected component of a graph as

async await performance?

老子叫甜甜 提交于 2019-12-17 07:30:47
问题 (Just a theoretical question - for non-gui apps) Assuming I have this code with many awaits : public async Task<T> ConsumeAsync() { await A(); await b(); await c(); await d(); //.. } Where each task can take a very short period of time , Question (again , theoretical) There could be a situation where the overall time dealing with all those "releasing back threads" and "fetching threads back" ( red & green here :) Is taking more time than a single thread which could done all the work with a

Is window really global in Javascript?

本秂侑毒 提交于 2019-12-17 04:29:15
问题 Take this piece of Javascript in a browser: <script> console.log(window.someThing); var x = 12; function foo() { window.otherThing = x; } </script> Inside foo we can access window , we all know that, but why exactly? Is it some kind of special global variable? Or does the "root scope" (inside the script tag) have it as an implicit local variable and is it simply "closure-inherited" as any other local variable (like x above) can be? And how does that concur with variables declared directly

Interface or abstract class?

自作多情 提交于 2019-12-17 03:20:09
问题 For my new Pet-Project I have a question for design, that is decided already, but I want some other opinions on that too. I have two classes (simplified): class MyObject { string name {get;set;} enum relation {get;set;} int value {get;set;} } class MyObjectGroup { string name {get;set;} enum relation {get;set;} int value {get;set;} List<MyObject> myobjects {get;set;} } Later in the Project MyObjectGroup and MyObject should be used equally. For this I could go two ways: Create an interface:

Why are C++ inline functions in the header?

瘦欲@ 提交于 2019-12-17 02:18:10
问题 NB This is not a question about how to use inline functions or how they work, more why they are done the way they are. The declaration of a class member function does not need to define a function as inline , it is only the actual implementation of the function. For example, in the header file: struct foo{ void bar(); // no need to define this as inline } So why does the inline implementation of a classes function have to be in the header file? Why can't I put the inline function the .cpp