HOW TO GET RID OF SEGMENTATION FAULT? #include <stdio.h> #include <stdlib.h> #in
ID: 663977 • Letter: H
Question
HOW TO GET RID OF SEGMENTATION FAULT?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[])
{
int x,j;
int z;
if(argc < 2)
printf(" Usage: ./program num num ");
for (x = 1; x <= argc; x++)
{
for(j = 0; j <= strlen(argv[x]); j++){
printf(" %s ",argv[x]);
if(argv[x][j] == '0' || argv[x][j] == '1')
{
z = z + 1;
printf("Mnemonics for %s are: None, %s contains a digit %c ", argv[x], argv[x], argv[x][j]);
break;
}
else if(argv[x][j] != '0' && argv[x][j] != '1' && argv[x][j] != '2' && argv[x][j] != '3' && argv[x][j] != '4' && argv[x][j] != '5' && argv[x][j] != '6' && argv[x][j] != '7' && argv[x][j] != '8' && argv[x][j] != '9' && argv[x][j] != '')
{
z = z + 1;
printf("Mnemonics for %s are: None, %s contains at least one alphabetic character ", argv[x], argv[x]);
break;
}
}
if(j == strlen(argv[x]))
printf("NOW ENTERING FUNCTION");
}
}
Explanation / Answer
SEGMENTATION FAULT:
Segfaulting mean trying to manipulate a memory location that shouldn't be accessible to the application.
That means that your problem can come from three cases :
Trying to do something with a pointer that points to NULL;
Trying to do something with an uninitialized pointer;
Trying to do something with a pointer that pointed to a now deleted object;
1) is easy to check so I assume you already do it as you nullify the pointers in the vector. If you don't do checks, then do it before the delete call. That will point the case where you are trying to delete an object twice. 3) can't happen if you set NULL to the pointer in the vector.
2) might happen too. In you case, you're using a std::vector, right? Make sure that implicit manipulations of the vector (like reallocation of the internal buffer when not big enough anymore) doesn't corrupt your list.
So, first check that you delete NULL pointers (note that delete(NULL) will not throw! it's the standard and valid behaviour! ) - in your case you shouldn't get to the point to try to delete(NULL). Then if it never happen, check that you're not having your vector fill with pointers pointing to trash. For example, you should make sure you're familiar with the [Remove-Erase idiom][1].
reasons for segmentation fault in C can be:-
1. If we use data instead of address (integer to pointer conversion) !
nt s;
scanf("%d", s);
^
|_______ &s should be placed
also you can only write format specifier inside scanf
ex:- scanf("hello %s", &s); // this will cause seg fault
2. If we allocate wrong size during memory allocation !
struct node* newNode=(struct node*) malloc(sizeof(struct node*));
// ^ * should not be
// present as it will become pointer
// hence size of ptr(4) will be allocated instead of the whole structure
3. If we are freeing memory twice !
free(ptr);
free(ptr);
4. If we have not allocated memory for pointer !
char *s="abc"; // this will work
char *s="Icandoitbutucan't"; // for long string core dump can occur
// always use char s[20];
5. If we try to access I/O devices or other hardware components directly by using address. ex:- VGA !
char *vga=(char *) 0xB8000; // In some compilers such as turboC this may work
6 . Stack Overflow !
main()
{
main(); // as recursion works like LIFO
// recursive procedure should be called inside if-else condn
}
7. When we try to write data in the array beyond boundary limit !
int a[2]={0,1};
printf("%d ", a[3]); // this will work. no problem
a[3]=55; // this can cause seg fault
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.