Posts

Showing posts from 2013

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

Image
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...

error: stray '\' in program

 stray.c:6: error: stray '\315' in program   stray.c:7: error: stray '\242' in program   stray.c:8: error: stray '\234' in program  If you are getting error messages like above in your C/C++ program, then don't worry. ;-) Perhaps you have copied the code from somewhere. If your code is small ,then the simplest solution is that you should start typing it from zero. That is , delete all the code ( the lines you know you have copied) and re-type it. If your code is lengthy and you do not want to type it, then you should use this software : Hex Editor Wiki You may be wondering that why this problem even occurred. The reason for this is that your code might contain some hidden octal (not-readable) characters which the compiler cant understand and you can not see. They are present in your code because you have perhaps copied it from somewhere. So, simply you can retype the same code to solve the problem. ----- Happy Coding :-)