Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, December 18, 2011

Segmentation fault causes and solution

Well, this "SEGMENTATION FAULT" error encountered me and my friends while doing network programming in C on a Linux platform.

Cause for this error : A segmentation fault occurs mainly when our code tries to access some memory location which it is not suppose to access.

For example :
  1. Working on a dangling pointer.
  2. Writing past the allocated area on heap.
  3. Operating on an array without boundary checks.
  4. Freeing a memory twice.
  5. Working on Returned address of a local variable
  6. Running out of memory(stack or heap)
So, avoid / debug above errors to get rid of this






[via]If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

Why main() should not have void as return type ?

Main() is actually defined in the libraries as int main() but not as void main() . This is the main reason we should use it with return type int. Even the ANSI standards suggests us to use int main() . If this is not followed, there is a chance of stack corruption.

Here is a most famous question .. It works fine with void as return type. So why should I go with int ??

it may work sometimes because of the existing garbage values. But no one calls main() in fact, when we execute a program, automatically the predefined callers initiates main() by calling it. This process involves a main() as called function , a shell script and a caller function.

For more detailed answer goto Go4Expert.





If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

Problem in using scanf() before fgets() or gets() in C | Solution


Problem :

code :
#include <stdio.h> 
#include <string.h> 
int main() 
{ 
      int n; 
      char buff[100]; 
      memset(buff,0,sizeof(buff)); 
      printf("Enter a number:"); 
      scanf("%d",&n); 
      printf("You entered %d \n",n); 
      printf("\n Enter a name:"); 
      fgets(buff,sizeof(buff),stdin); 
      printf("\n The name entered is %s\n",buff); 
      return 0; 
}


output :


~/home $ ./aa  
Enter a number:123 
You entered 123  
 
 Enter a name: 
 The name entered is  
 
 ~/home $ ./aa  
Enter a number:123abc456 
You entered 123  
 
 Enter a name: 
 The name entered is abc456


As you can see above, I ran the executable twice and I got some weird results :

  1. In the first run, the execution did not stop at stdin when the fgets() API was being executed. So, I could not enter a name and hence no name was displayed.
  2. In the second run also the execution did not stop at stdin when fgets() API was being executed. So, I could not enter a name but the weirder part is that the last line of the output shows few final bytes of the input given to scanf() API being stored as the value of name.
---> Using fflush(stdin); may work in old compilers but not in new and advanced compilers like gcc.

Solution :

Code:
#include <stdio.h> 
#include <string.h> 
  int main() 
  { 
      int n; 
      char buff[100]; 
      memset(buff,0,sizeof(buff)); 
      printf("Enter a number:"); 
      scanf("%d",&n); 
      printf("You entered %d \n",n); 
      getchar(); 
      printf("\n Enter a name:"); 
      fgets(buff,sizeof(buff),stdin); 
      printf("\n The name entered is %s\n",buff); 
      return 0; 
  }
Lets look at the output :

Code:
~/home $ ./aa  
Enter a number:123 
You entered 123  
 
 Enter a name: globalsoftbay 
 
 The name entered is globalsoftbay

Adding getchar() solves the problem. But I don't think it is ideal solution. If you people find this working / not working, just comment below. If anyone know the ideal solution, fee free to post.




If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged

Wednesday, October 12, 2011

Difference Between Constant Pointers and Pointer to Constants

1) Constant Pointers : These type of pointers are the one which cannot change address they are pointing to. This means that suppose there is a pointer which points to a variable (or stores the address of that variable). Now if we try to point the pointer to some other variable (or try to make the pointer store address of some other variable), then constant pointers are incapable of this.

A constant pointer is declared as : 'int *const ptr' ( the location of 'const' make the pointer 'ptr' as constant pointer)

2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.

A pointer to a constant is declared as : 'const int *ptr' (the location of 'const' makes the pointer 'ptr' as a pointer to constant.





If you enjoyed this post, make sure you subscribe to my RSS feed! Comments are encouraged