I'm trying to compile my code and I feel it works perfectly fine and I can get it to compile. However, when I do I'm getting Segment fault error and I don't see where the error is in my code.
The error i'm getting is Segment fault: 11 I've looked this up and I know it has to do with memory allocation but haven't been able to find where in the code do I need to fix my memory allocation and fix the errors I have on here.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
/*The Main Function Start*/
void main(int argc, char *argv[])
{
/*Storing The Process Id*/
pid_t pid;
int j;
int status = 0;
/*process of forking*/
if (argc == 1){
fprintf(stderr,"Usage: ./hw1 <starting value>\n");
}
int n = atoi(argv[1]);
pid=fork();
if (pid == -1){
printf("Error in forking....\n");
exit(0);
}
/*Child process*/
if (pid == 0)
{
printf("Child PID: %d\n",getpid());
while (n != 1){
printf("%d ",n);
if (n % 2 == 0){
n = n/2;
}
else {
n = 3*n + 1;
}
}
printf("1\n");
}
else{
printf("Parent PID: %d\n",getpid());
/*Waiting for the child to finish*/
wait(0);
}
exit(0);
}