<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: J. Marchán</title>
    <description>The latest articles on DEV Community by J. Marchán (@devmarchan).</description>
    <link>https://dev.to/devmarchan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F109367%2F17282f16-bf91-4ab2-8675-6d34331f69c9.jpg</url>
      <title>DEV Community: J. Marchán</title>
      <link>https://dev.to/devmarchan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmarchan"/>
    <language>en</language>
    <item>
      <title>Should input parameters be checked?</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Mon, 30 Mar 2020 15:55:49 +0000</pubDate>
      <link>https://dev.to/devmarchan/should-input-parameters-be-checked-3o97</link>
      <guid>https://dev.to/devmarchan/should-input-parameters-be-checked-3o97</guid>
      <description>&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;In the last few months, I have been struggling with this problem: Should input parameters be checked? Hundreds of warnings were thrown by static analysis tool in a large base code, most of them regarding pointers which could be null or variables which could index an array outside its bounds. In order to certificate the software, those warnings must be removed. Quickly, the whole team started to code checks in every problematic variable. We were falling into a trap and the code quality started going downhill. Nobody had a magic answer (because there isn’t) and there was no time to do a deep analysis.&lt;/p&gt;

&lt;p&gt;Suddenly, a lot of questions arose: Should the input data be validated? Should it be the caller? Should be the function called? Without noticing, these questions lead to another highly related topic: &lt;strong&gt;How much redundancy and robustness should software have?&lt;/strong&gt; The very first question can not be answered if the last one has not been discussed, which is, indeed, the real question.&lt;/p&gt;

&lt;p&gt;Fortunately, there are people who have faced this problem before. I strongly encourage to read the references. Here there are some few key points: &lt;/p&gt;

&lt;h2&gt;
  
  
  Searching an answer
&lt;/h2&gt;

&lt;p&gt;The problem and the solution have to be present in the first stages of the software design and every member of the team should be aware of it. What is more, &lt;strong&gt;it is not even a technical question, it is a business one&lt;/strong&gt;. It is different whether your software can fail and reboot or if it is not an option. It is different whether your software has a hard time restrictions or if it does not matter if it takes 5 ms more. It is different if you are writing an API or safety-critical piece of code. &lt;strong&gt;In other words: It is a requirements thing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If a clear decision is not taken and the decision is left to the programmers as they go, errors will be checked on the same parameter three or four levels into the call stack and the solution implemented through the application won’t be coherent and will become crappy.&lt;/p&gt;

&lt;p&gt;So then, how to check input data? So far, we can talk about two approaches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Design by Contract (DBC)
&lt;/h3&gt;

&lt;p&gt;A concept developed by Bertrand Meyer as a design technique “that focuses on documenting and agreeing to the rights and responsibilities of software modules to ensure program correctness”. It is a contract by which a routine may have some expectations:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Preconditions&lt;/em&gt;: The routine’s requirement. According to [HT00], “a routine should never get called when its preconditions would be violated” and &lt;strong&gt;“it is the caller’s responsibility to pass good data. It is its responsibility.”&lt;/strong&gt; If the caller can’t assure pass good data, has to decide what to do, how to handle it. Again, it is a requirements thing, a design decision.&lt;br&gt;
&lt;em&gt;Postconditions&lt;/em&gt;: The state of the world when the routine is done.&lt;/p&gt;

&lt;p&gt;Another benefit of this technique is to detect problems as soon as you can, being able to crash the program earlier, which is better than continuing with corrupted data. “When your code discovers that something that was supposed to be impossible just happened, your program is no longer viable”&lt;/p&gt;

&lt;p&gt;But not all languages support DBC in the code. In languages such as C, writing the contract as a comment gets you still a very real benefit. Moreover, it is possible to emulate this using assertion. We will see later.&lt;/p&gt;

&lt;p&gt;A lot of authors follow the idea that run-time checks add unneeded complexity and that it is worthy to replace it with asserts and thorough integration testing. One of the classics’ uses of assertions is to check assumptions like that an input parameter’s value falls within its expected range (or an output parameter’s value does) or that a pointer is non-null.&lt;/p&gt;

&lt;p&gt;Nevertheless, a function that tests a condition and crashes if the condition is not fulfilled may be OK for debugging, but in most cases is not flexible enough in production code. Or maybe yes, it is the only sensible option. Again: it is a design-architecture-requirements thing. One workaround could be to use assertion in the development phase, while debugging, and turn it off in release code. The problem is that in order to certificate the software, with that workaround we would be in the same place. Although the code quality will be better, a static analysis tool would be throwing the same warnings.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defensive Programming
&lt;/h3&gt;

&lt;p&gt;Some may say that DBC and defensive programming are kind of the opposite, but [McC04] includes assertions to check preconditions inside its chapter of Defensive Programming, as a way to handle garbage-in:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“The main idea is that if a routine is passed bad data, it won’t be hurt, even if the bad data is another routine’s fault”&lt;/strong&gt;. You take responsibility even when it is other’s fault. &lt;a href="https://twitter.com/rsrajan1"&gt;@rsrajan1&lt;/a&gt; gives us another good definition: &lt;a href="https://medium.com/swlh/why-defensive-programming-is-the-best-way-for-robust-coding-cfa790fe04cd"&gt;“The idea is not to write code that never fails. That is a utopian dream. The idea is to make the code fail beautifully in case of any unexpected issue”.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main way to handle garbage-in is checking the values of all routine input parameters and once you’ve detected an invalid parameter, deciding how to handle bad inputs.&lt;/p&gt;

&lt;p&gt;Depending on the situation, you might choose any of a dozen different approaches. [McC04] in Section 8.3 Error-Handling Techniques, describes in detail some of them (return neutral value, substitute the next piece of valid data, return the same answer as the previous time…). How to decide? Again, McConnel insists: &lt;strong&gt;“Deciding on a general approach to bad parameters is an architectural or high-level design decision and should be addressed at one of those levels.”&lt;/strong&gt; The way in which errors are handled affects the software’s ability to meet requirements related to the correctness, robustness, and other nonfunctional attributes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The conclusion
&lt;/h2&gt;

&lt;p&gt;To summarize, the most important thing to have into account is the need for high-level decisions about what to do with bad input. Once this is clear, take advantage of design techniques such as BDC of features of the language to approach the solution. In the next post, a solution based on C and assertions would be proposed.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;[HT00] Andrew Hunt and David Thomas. 2000. The pragmatic programmer: from journeyman to master. Addison-Wesley Longman Publishing Co., Inc., USA.r&lt;/p&gt;

&lt;p&gt;[McC04] Steve McConnell. 2004. Code Complete, Second Edition. Microsoft Press, USA&lt;/p&gt;

&lt;p&gt;[Mey97] Bertrand Meyer. Object-Oriented Software Construction. Prentice Hall, Englewood Cliffs, NJ, second edition, 1997.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://myram.xyz/should-input-parameters-be-checked"&gt;Original post in myram&lt;/a&gt;&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>softwareengineering</category>
      <category>c</category>
    </item>
    <item>
      <title>Overflow in integer arithmetic with C</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Tue, 11 Jun 2019 18:47:48 +0000</pubDate>
      <link>https://dev.to/devmarchan/overflow-in-integer-arithmetic-with-c-4136</link>
      <guid>https://dev.to/devmarchan/overflow-in-integer-arithmetic-with-c-4136</guid>
      <description>&lt;h2&gt;
  
  
  Overflow
&lt;/h2&gt;

&lt;p&gt;Sometimes, the results of working with integers may go outside the range that can be represented, that is, when the result of operating on two k-bit numbers is a (k+1)-bit result. Static analysis tools such as Polyspace or Sonar may also trigger a warning on an operation or in the worst case, you come upon an error in run time.&lt;/p&gt;

&lt;p&gt;Typically, addition and multiplication are the sources of overflow, but it can appear with subtraction and the abs function.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to overcome the problem
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Up-casting to a larger size.
&lt;/h3&gt;

&lt;p&gt;The first proposal requires to know the maximum (or minimum) value of the types you are working with. If so, it is possible to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include &amp;lt;stdint.h&amp;gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="nf"&gt;add_without_overflow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int32_t&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int32_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;INT16_MAX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT16_MAX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;INT16_MIN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;INT16_MIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This piece of code upcast the addition to larger type size, from int16 to int32, from two k-bit numbers to (k+1)-bit number. Afterward, check for overflow. That is, if your result fits in the k-bit number, you got it.&lt;/p&gt;

&lt;h3&gt;
  
  
  To compare with a well-known domain value
&lt;/h3&gt;

&lt;p&gt;Maybe is not so easy to know the maximum or minimum or a certain type in the target platform. In that case, an easy way to overcome is to check the operands with a maximum or minimum of your specific domain. Suppose that we are operating on two variables which stand for the weight of two loads in grams. But we are guaranteed (by system requirements) that no load can be heavier than 5.000 grams.&lt;/p&gt;

&lt;p&gt;It can be just put:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="nf"&gt;add_without_overflow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int16_t&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;MAX_LOAD_WEIGHT&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;amp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;MAX_LOAD_WEIGHT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;z&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Furthermore, that way the correctness of the data is checked.&lt;/p&gt;

&lt;p&gt;Nevertheless, this approach have a drawback in my opinion. You need you be very careful with the domain ranges. Imagine that the maximum of the load could be 40,000. An addition of two the heaviest loads (40,000+40,000) results in an overflow.&lt;/p&gt;

&lt;p&gt;In my opinion, that would be a valid solution when your domain values are far away from the limits of you type size.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;Here can be found more precise and detailed explanations&lt;/p&gt;

&lt;p&gt;&lt;a&gt;Understanding and Preventing Overflow&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.fefe.de/intof.html"&gt;Catching overflows in C&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.cs.utah.edu/~regehr/papers/overflow12.pdf"&gt;Understanding integer overflow in C/C++&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://myram.xyz/overflow-in-integer-arithmetic-with-c/"&gt;Original post in myram&lt;/a&gt;&lt;/p&gt;

</description>
      <category>overflow</category>
      <category>c</category>
    </item>
    <item>
      <title>Software as a product of intellectual activity – Reflections on software development</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Wed, 28 Nov 2018 17:37:31 +0000</pubDate>
      <link>https://dev.to/devmarchan/software-as-a-product-of-intellectual-activity--reflections-on-software-development-1h9g</link>
      <guid>https://dev.to/devmarchan/software-as-a-product-of-intellectual-activity--reflections-on-software-development-1h9g</guid>
      <description>&lt;h2&gt;
  
  
  What is software development?
&lt;/h2&gt;

&lt;p&gt;Software development involves far more than the mere production of code. It goes beyond writing instructions and knowing specific syntax and grammar rules. So, we may well ask ourselves: &lt;strong&gt;what is software and how is it developed?&lt;/strong&gt; One of the best examinations of this subject was done by Peter Naur – creator of the BNF notation and winner of a Turing award – in his essential article “Programming as Theory Building”. Here’s a summary of his idea.&lt;/p&gt;

&lt;p&gt;Naur uses Ryle’s notion of theory as a starting point. In The concept of Mind, a classic work of philosophy on the nature of the mind and human behavior, Ryle develops his notion of theory as part of his analysis of intellectual activity and the manner in which this differs from activity that is merely intelligent. What characterizes the first versus the second is that for intellectual activity the person builds a theory. This theory is understood as the knowledge a person must have in order not only to do things intelligently and well according to certain criteria, but also to be able to explain, answer questions, argue or justify the activity of concern. Having a theory allows the person to detect and correct lapses, to learn from examples, and so on. It should be noted that this notion does not rely on the idea that intelligent behavior depends on following or adhering to rules or methods. For example, to understand Newton’s theory of mechanics, it does not suffice to know that force is equal to mass times acceleration; rather, a person must have an understanding of the way in which Newton’s laws apply to certain aspects of reality so as to be able to recognize and apply the theory to other similar phenomena.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From Ryle, Naur borrows the idea of theory and argues that what has to be built by the programmer is a theory of how certain aspects of the world will be modeled&lt;/strong&gt;, represented and handled by a software system. This person is able to respond to any demand for modification or adaptation to any change in requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implications for software maintenance
&lt;/h2&gt;

&lt;p&gt;A software program’s life after delivery, that is, its maintenance (remember that this is what occupies most of the life cycle) depends on the people who are responsible for said program having this theory. If the programmer team that possesses the theory is dissolved, the deep knowledge of that materialization of a real problem in code form is lost, producing the death of the software program. The program can continue to be executed, but modifications and enhancements will become increasingly expensive and technical costs will increase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The theory is a collective property shared between members of the original programmer team&lt;/strong&gt;. Naur argues that for new generations of programmers to prolong the life of software, it is more important for them to work closely with the original programmers – to learn how the software works and how unexpected program reactions and modifications can be understood within the program theory – than it is for them to become familiar with the code and documentation (though this will certainly help). Thus, communication and the human factor become an important part of software development.&lt;/p&gt;

&lt;p&gt;One important consequence of this view is that rebuilding a software program’s theory from documentation alone is impossible. Or the classic Brooks Law, which states that adding manpower to a late software project makes it later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;The fact that programmers spend a considerable amount of development time trying to understand code is a problem intrinsic to software development. What we as programmers are unconsciously doing – though we may know nothing of Naur or Ryle’s Building Theory – is trying to rebuild the theory, to create that mental vision. And this is so because programming belongs to the realm of intellectual, and not merely productive, activity. Therefore, it is still a particularization of the traditional creation problem. An experienced engineer, with a deep knowledge of the problem domain, is able to solve related questions in a way that is consistent with the theory. Why are they always coming up with better and faster ideas? The Austrian writer Stefan Zweig, who was interested in the mystery of creation (artistic, not software) throughout his life, once said that “of all the mysteries of the universe, none is more profound than that of creation […] and who could say where ideas come from?” At least in the case of software, we can say that theory plays a part.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://myram.xyz/software-as-product-of-intellectual-activity/"&gt;Original published in myram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;References&lt;/em&gt;&lt;br&gt;
– Peter Naur, Programming as Theory Building&lt;br&gt;
– Ryle, G. The Concept of Mind&lt;br&gt;
– Javier Garzás, Comprendiendo qué es crear software&lt;/em&gt;&lt;/p&gt;

</description>
      <category>software</category>
      <category>development</category>
      <category>naur</category>
      <category>theory</category>
    </item>
    <item>
      <title>Unequally distributed quantity</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Tue, 06 Nov 2018 20:04:19 +0000</pubDate>
      <link>https://dev.to/devmarchan/unequeally-distributed-quantity-34n5</link>
      <guid>https://dev.to/devmarchan/unequeally-distributed-quantity-34n5</guid>
      <description>&lt;p&gt;Suppose that you have to divide or to distribute a quantity in three (maybe a rent payment), but you want to do it unequally.&lt;/p&gt;

&lt;p&gt;It can be done more or less mentally, but I wanted to have&lt;br&gt;
quickly all the possibilities and decided to write some code to do it.&lt;/p&gt;

&lt;p&gt;I though how it is done mentally. For instance, dividing 990 units in 3 parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You have three parts of 330 each one&lt;/li&gt;
&lt;li&gt;Subtract 5 to one and add it to another&lt;/li&gt;
&lt;li&gt;Repeating that you obtain different possibilities to divide initial quantity&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Translation to code.
&lt;/h2&gt;

&lt;p&gt;First approximation to the problem was to establish a minimum that each person would pay. Let’s to say 250. 250×3= 750, so it remained 240 units to share out.&lt;/p&gt;

&lt;p&gt;I wanted a granularity of 5 units, so there was in fact a 24/5=48 items (of 5 units) to share out.&lt;/p&gt;

&lt;p&gt;It was not trivial for me to lay out the problem until I realized that this matched with some well known combinatorial problem that I didn’t remember. After a search I found exactly what I was looking for: &lt;a href="https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)" rel="noopener noreferrer"&gt;Start and bars&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;It can be used to solve many simple counting problems, such as how many ways there are to put n indistinguishable balls into k distinguishable bins.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It can be easily done with Python using &lt;a href="https://docs.python.org/2/library/itertools.html#itertools.combinations" rel="noopener noreferrer"&gt;combinations function&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;itertools&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;combinations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;itertools&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;combinations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;calculate_rent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;combinations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;combinations(n,k)&lt;/em&gt; return &lt;em&gt;r&lt;/em&gt; length subsequences of elements from the input iterable n. Afterwards, we only want subsequences whose sum of the k bins be equal to n.&lt;/p&gt;

&lt;p&gt;Just left to write some auxiliary code in order to print the list. Due to what we have obtained is a list of ways  to put n indistinguishable balls (48) into k distinguishable bins (3), it is necessary to multiply each ball by our granularity (5) and sum the minimum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;990&lt;/span&gt;
&lt;span class="n"&gt;minimum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;250&lt;/span&gt;
&lt;span class="n"&gt;people&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;granularity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;remain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;minimum&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;people&lt;/span&gt;
&lt;span class="n"&gt;rent_combinations&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculate_rent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;remain&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;granularity&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;people&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;rents&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rent_combinations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;minimum&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rents&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is displayed a partial screen-shot of the 192 total elements of the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3m68zcn7qkl50ntrmx7f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3m68zcn7qkl50ntrmx7f.jpg" alt="Output" width="760" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://myram.xyz/unequally-distributed-divide-quantity-python/" rel="noopener noreferrer"&gt;Original post in myram&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>combinatorics</category>
    </item>
    <item>
      <title>A dummy blockchain implementation in C (II)</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Thu, 25 Oct 2018 18:22:07 +0000</pubDate>
      <link>https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-ii-36eh</link>
      <guid>https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-ii-36eh</guid>
      <description>&lt;p&gt;&lt;em&gt;&lt;a href="https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-i-3m97"&gt;A dummy blockchain implementation in C (I)&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Parties
&lt;/h2&gt;

&lt;p&gt;All the ingredients are ready, so now it is time to code the main function. As I already said, it is going to be a voting system. For that, I have created the next enum with all the parties available in the electoral system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;party_code_t&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="n"&gt;GOOD_PARTY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MEDIOCRE_PARTY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;EVIL_PARTY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;MAX_PARTIES&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;party_code&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And a function which randomly gets a vote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;party_code&lt;/span&gt; &lt;span class="nf"&gt;get_vote&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;MAX_PARTIES&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  2. First block: genesis
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;NODE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;DATA&lt;/span&gt; &lt;span class="n"&gt;genesis_element&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;transaction&lt;/span&gt; &lt;span class="n"&gt;genesis_transactions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_vote&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;block_t&lt;/span&gt; &lt;span class="n"&gt;genesis_block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;genesis_transactions&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;genesis_transactions&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="n"&gt;genesis_element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genesis_block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;genesis_element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In order to create a new element are necessary three steps:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. To create the transaction. We create a transaction getting the name of the party.
2. To create the block with its three fields: previous hash, current hash and a transaction.
3. To create the node element of the list (the block) and to insert it to the list.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It is common to call the first block in a blockchain as genesis. As there is no previous block, we set the previous block hash to zero.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Adding votes
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight c"&gt;&lt;code&gt;  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;previous_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;genesis_element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;previous_block_hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;nvotes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="n"&gt;DATA&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;
        &lt;span class="n"&gt;block_t&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;transaction&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;get_vote&lt;/span&gt;&lt;span class="p"&gt;()};&lt;/span&gt;

        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;block_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;transactions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;previous_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;block_hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;el&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once the first element has been inserted, it is possible to create a loop to create as many blocks as wanted. It follows the three steps from above.&lt;/p&gt;

&lt;p&gt;That is all. After compiling and executing we can read the output in the terminal. Each vote (or block) is displayed below the previous one, and it is formed by the hash of the previous vote, the current hash and the name of the voted party. Its current hash it is formed by all the votes previously submitted.&lt;/p&gt;

&lt;p&gt;You can see the final &lt;a href="https://github.com/jmarchanloro/1-blockchain"&gt;code here&lt;/a&gt;. The core is the snippets showed in the post, but more auxiliary code has been added, mainly to print the linked list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PgEIVNth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/myram.xyz/wp-content/uploads/2018/09/output.jpg%3Fw%3D454%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PgEIVNth--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i2.wp.com/myram.xyz/wp-content/uploads/2018/09/output.jpg%3Fw%3D454%26ssl%3D1" alt="Ten votes stored in a blockchain"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://myram.xyz/c-blockchain-implementation-2/"&gt;Original post in myram&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;em&gt;Disclaimer&lt;/em&gt;: This article is full of simplifications and abstractions in order to get the fundamental. Although one of the key points of blockchain is that database isn’t stored in any single location but it is shared and transactions are spreading to a P2P network of nodes, here is obviated all the network part.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>c</category>
    </item>
    <item>
      <title>A dummy blockchain implementation in C (I)</title>
      <dc:creator>J. Marchán</dc:creator>
      <pubDate>Mon, 22 Oct 2018 16:46:55 +0000</pubDate>
      <link>https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-i-3m97</link>
      <guid>https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-i-3m97</guid>
      <description>&lt;h2&gt;
  
  
  Voting with Blockchain
&lt;/h2&gt;

&lt;p&gt;Hundreds of thousands of articles have been written about blockchain and how it is going to change the world. I am not to talk why I don’t understand how this can be possible. Neither Bitcoin, which (from my misinformed point of view) it is just a way to people trying to earn dollars: the ultimate goal of bitcoiners is wait enough time and change them to dollars (sincerely, where is the revolution?).&lt;/p&gt;

&lt;p&gt;But this is not the topic. &lt;strong&gt;This is a description step by step of a blockchain algorithm implementation in C&lt;/strong&gt;. And in order to vary a bit, instead a money transactions it is going to emulate a voting system.&lt;/p&gt;

&lt;p&gt;Firstly, it is necessary to understand the basic concepts of blockchain. If you still don’t know it I recommend you &lt;a href="https://blockgeeks.com/guides/what-is-blockchain-technology/" rel="noopener noreferrer"&gt;this&lt;/a&gt;, &lt;a href="https://medium.com/the-mission/a-simple-explanation-on-how-blockchain-works-e52f75da6e9a" rel="noopener noreferrer"&gt;this&lt;/a&gt; and &lt;a href="https://www.youtube.com/watch?v=SSo_EIwHSd4&amp;amp;feature=youtu.be" rel="noopener noreferrer"&gt;this 5 minutes introduction video&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now all we know what a transaction, block and hash are we are ready: &lt;strong&gt;Blockchain is very easy in its core&lt;/strong&gt;. It is formed by a well-known data structure in computer science: Linked list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flq586bo7lgiovi1rydd3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flq586bo7lgiovi1rydd3.png" alt="A linked list is a sequence of nodes. Each node contains a bulk of data and a link to the next node of the list." width="408" height="41"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The block
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;Block_T&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="n"&gt;previous_block_hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="n"&gt;block_hash&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;transaction&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;block_t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is declared a block with the hash of the previous block, the hash of the current block and a transaction. Instead of a set of transaction, the block has just one transaction: a single string of characters.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The hash
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;string_hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* This is the djb2 string hash function */&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5381&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="sc"&gt;'\0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function, copied from somewhere, take a string of characters and return an integer which is the unique hash.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. The list
&lt;/h3&gt;

&lt;p&gt;I will not explain either how a list woks since it is a very basic data structure and it is easy to find resources on Internet. However, it was not so easy for me to find a clear, clean and simply implementation of lists in C. So I take one and adapted it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;block_t&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;DATA&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;DATA&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="n"&gt;NODE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data of the nodes of our list is the block_t structure defined previously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;NODE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NODE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DATA&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NODE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;NODE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NODE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;get_list_transactions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NODE&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;list_trans&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the main functions of the list is add, in order to insert new nodes at the end, and print, which is going to show the node data information, which is the block.&lt;/p&gt;

&lt;p&gt;Those are the main ingredients. In the &lt;a href="https://dev.to/devmarchan/a-dummy-blockchain-implementation-in-c-ii-36eh"&gt;next post&lt;/a&gt; I am going to put everything together to see a blockchain running.&lt;/p&gt;

&lt;p&gt;(Original post in &lt;a href="https://myram.xyz/c-blockchain-implementation-1/" rel="noopener noreferrer"&gt;myram&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>blockchain</category>
      <category>c</category>
    </item>
  </channel>
</rss>
