noob question here: I'm trying to write a simple menu interface, but I keep getting a segmentation fault error and I can't figure out why.
#include <stdlib.h>
#include <stdio.h>
int flush(); int add(char *name, char *password, char *type); int delete(char *name);
int edit(char *name, char *password, char *type, char *newName, char *newPassword, char *newType);
int verify(char *name, char *password);
int menu(){
int input;
char *name, *password, *type, *newName, *newPassword, *newType;
printf("MAIN MENU \n ============\n");
printf("1. ADD\n");
printf("2. DELETE\n");
printf("3. EDIT\n");
printf("4. VERIFY\n");
printf("5. Exit\n");
printf("Selection:");
scanf("%d", &input);
flush();
switch (input){
case 1:
printf("%s\n", "Enter Name:");
scanf("%s", name);
flush();
printf("%s\n", "enter password" );
scanf("%s", password);
flush();
printf("%s\n","enter type" );
scanf("%s",type);
add(name, password, type);
menu();
break;
case 2:
printf("Enter Name:" );
scanf("%s",name);
flush();
delete(name);
menu();
break;
case 3:
printf("Enter Name:\n");
scanf("%s",name);
flush();
printf("Enter Password\n");
scanf("%s", password);
flush();
printf("enter type:\n");
scanf("%s", type);
flush();
printf("enter your new username:\n");
scanf("%s",newName);
flush();
printf("enter your new password\n");
scanf("%s", newPassword);
flush();
printf("enter your new type\n");
scanf("%s",newType);
flush();
edit(name, password, type, newName, newPassword, newType);
menu();
break;
case 4:
printf("Enter Name\n");
scanf("%s",name);
flush();
printf("Enter Password\n");
scanf("%s",password);
flush();
verify(name, password);
menu();
break;
case 5:
return 0;
default:
printf("invalid input, please select from the following:\n");
menu();
}
return 0;
}
int flush(){
int ch;
while ((ch = getchar()) != EOF && ch != '\n') ;
return 0;
}
I get the segmentation fault after entering two fields, in any menu option
Use debuggers to diagnose segfaults Start your debugger with the command gdb core , and then use the backtrace command to see where the program was when it crashed. This simple trick will allow you to focus on that part of the code.
Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump. It is an error indicating memory corruption.
A segmentation fault usually occurs when you try to access data via pointers for which no memory has been allocated. It is thus good practice to initialize pointers with the value NULL, and set it back to NULL after the memory has been released.
When Segmentation fault 11 occurs, it means that a program has attempted to access a memory location that it's not allowed to access. The error can also occur if the application tries to access memory in a method that isn't allowed.
You need to initialize your pointers. Alternatively, use stack-allocated arrays.
For example, instead of char *name
, do char name[20]
. (Note that this will limit your input to 19 characters; use a larger buffer if necessary.)
Right now, you are passing uninitialized pointers into scanf()
which effectively means that scanf()
is going to write to an undefined area of memory. It might work on one execution and then fail on the next. It might corrupt memory elsewhere in the process' address space.
Don't use uninitialized variables, and consider turning up your compiler warnings as high as they will go; the compiler can catch errors like this and emit a warning.
Instead of using *name, *password,.. use name[100], password[100],... If you want name, password, .. to be pointer then allocate memory using malloc or calloc before calling scanf.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With