Skip to main content

Posts

Installing Golang : The fast way

Steps only for Ubuntu. Other OS quick installation guide coming up soon! So, this article is useful when you are in hurry and obviously, want to trust me :) There are basically two ways to install golang - one is using the classical apt-get method and the other one is installing from source. he First method     $ sudo apt-get update     $ sudo apt-get install golang This will pretty much do the thing. Just two commands. You can verify the installation just by checking this:     $ go env GOROOT This command will output the directory name where go is installed. Usually, it will show /usr/lib/go. You can check go version:     $ go version     go version go1.5.1 linux/amd64 You are set to use golang. Please don't forget to configure $GOPATH and $GOBIN directories explained below.  Note: This method will install go version which is included in os package. If you need to install particular version o...

Expandable Arrays in C : behind the code

Arrays. They are beautiful. In C, we always try to work through restrictions arrays have i.e the size of the array must be statically defined. We always want a dynamic array whose size can be decided at run-time. There are ways which we use widely for e.g by using pointers and then dynamically allocating memory to it using malloc or calloc. But depending on the situation, we might require more efficient and organizable ways. I will explain the concept with a series of examples as in how simple arrays evolved to the tricky expandable ones. The classic way is to use pointers This snippet will allocate a memory block of size = array_size(integer array). Though this is not what I wanted to explain because this way is too clumsy. Imagine if you need 10 dynamic arrays in your program, you have to write these four lines each time and you have to keep track of all the 10 sizes. A better way is to use structures: We can simply create a structure containing two elements: array_size and...

Stack Size : behind the code

How to know the stack size of a process in Linux? #Interview Question The stack word is very popular when it comes to C programming. It often intrigues us that how much is the stack size of a process and how to know it. The answer to this question can not be given without considering the "platform independent" thing. Well, we all are familiar with the word "platform independent". It is only word which comes to our mind when we don't know the answer. :P So, the stack size depends on the platform, the architecture - microprocessor is of how many bits - 16, 32, 64? But I will explain the answer with respect to Linux operating system. Method 1 The simplest method which I found is through cat /proc/-pid-/limits cat command is used to access a file. proc is a directory which contains information about all the running processes. -pid- is the process id of the process. We can know the process id of any process using ps -ef command. See man page of p...

error: invalid application of ‘sizeof’ to incomplete type ‘struct ’

list.c:47:39: error: invalid application of ‘sizeof’ to incomplete type ‘struct Litsnode’ So, I was trying to run a program based on linked list and this is what I got. This is a very silly problem. I am mentioning it here just to help those who got frustrated like me :-\ Let me show you the line where this error occurred : 46   struct Listnode * newnode; 47   newnode = (struct Listnode *) malloc(sizeof(struct Litsnode)); Now, View it properly. Check the name(spelling) of the structure mentioned to sizeof : " struct Li ts node ". Though the defined name of the structure was : " struct Listnode " That's it.  This happens all the time that we may type something wrong. The important thing is to identify it. :-) Feel free to give any suggestions :)

Recursion : behind the code

Recursion is a very popular concept widely used in programming. This approach can be used to solve those type of problems, in which a part of algorithm is to be repeated to get the solution. The discussion of this topic, here, is to clearly understand of how RECURSION works? It will be discussed in two sessions. The first session will simply help you to visualize the working pattern of Recursion and the second will explore the strategy to write algorithms using recursion. Before going through this article, you should have Understanding of control flow of function calls. Basic understanding of what ‘recursion’ is? How stack(internal) is used in C programs. Session 1: visualize recursion Now, to understand this concept, the simplest example is -- program of -- finding factorial of a number. int Factorial (int n) { if (n <= 1) { return 1; } else { // “fact” is an integer variable fact = n * Factorial (n-1) ; return (fact); } } Whe...

Segmentation Fault : behind the code

Segmentation fault Segmentation fault (core dumped) Did you just look at your laptop and said, "This is it buddy, don't check my patience, you are going to get hurt !!!" . If you are in this situation, then just try to understand the concept behind segmentation fault and forget the headache ;-) Segmentation fault is the exception which occurs when a program tries to access an invalid memory.  In simple words, it means accessing a memory which does not belong to you or you do not have authorization to access it.  To resolve this, you must know about GNU Debugger(GDB) , but if you are SHORT OF TIME  then first try following example: 1  #include<stdio.h> 2  int main() 3  { 4   int *p; 5   *p= 7; 6   printf("Output = %d\n",*p); 7  }  If we run this program, we will get Segmentation fault: user@gene-laptop:~$ gcc -g segment.c user@gene-laptop:~$ ./a.out Segmentation fault (core dumped) Th...

bash: ./a.out Permission denied

 bash: ./a.out Permission denied  sh: ./a.out Permission denied   Huh! Why the hell it is giving this error. It was working just fine previously. Reason 1: So, you may get this error sometime. I found this one when i tried to execute my executable file (a.out) on one of my friend's computer. When i checked it out thoroughly, I found out that normally in every executable file, there is a tab in properties of files. That tab is "Permissions". There is an option "Execute : Allow executing file as program". Just mark it true :-). a.out -> Right Click -> Properties -> Permissions -> Allow executing file as program Reason 2: Try running the file as root. You may not have permission to run the file, using sudo might work or change the permissions by using chmod . Note: This error might occur due to some other problems. I wrote this article in order to help those who have situations as described above. But please mention here if this kind of error is no...