Question

Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

Would u help me fixing this CODE With

Debugging Code with GDB

#include <stdio.h>
#include <stdlib.h>

#define SIZE (10)

typedef struct _debugLab
{
    int i;
    char c;
} debugLab;


// Prototypes
void PrintUsage(char *);
void DebugOption1(void);
void DebugOption2(void);

int main(int argc, char **argv)
{
    int option = 0;

    if (argc == 1)
    {
        PrintUsage(argv[0]);
        exit(0);
    }

    option = atoi(argv[1]);

    if (option == 1)
    {
        DebugOption1();
    }
    else if (option == 2)
    {
        DebugOption2();
    }
    else
    {
        PrintUsage(argv[0]);
        exit(0);
    }
}

void PrintUsage(char *command)
{
        fprintf(stderr, "Usage: %s [1 2]\n", command);
        fprintf(stderr, "           1 = Mode 1\n");
        fprintf(stderr, "           2 = Mode 2\n");
}

void DebugOption1()
{
    int i, j;
    int sum = 0;
    char charArray[SIZE];

    fprintf(stdout, "Here we are in DebugOption1()\n");

    
    for (i = 0; i < SIZE; i++)
    {
        sum += sum + i;
    }

    fprintf(stdout, "The sum of integers from 0 to %d is: %d\n", SIZE, sum);

}

void DebugOption2()
{
    debugLab *dl1 = NULL;
    debugLab *dl2 = malloc(sizeof(debugLab)); // Always check for a NULL Pointer after a call to malloc
    debugLab dl3;

    dl3.i = 37;
    dl3.c = 'A';
    dl2->i = 36;
    dl2->c = 'B';
    dl1->i =35;   // Oops! dl1 is NULL!
    dl1->c = 'C';

}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The solution to the above question is given below with screenshot of output.

=====================================================

I kept the logic simple and i have tested all outputs.

If there is anything else do let me know in comments.

====================================================

I have HIGHLITED THE BUG and corrected it just below.

============ CODE TO COPY ==============================

main.c

#include <stdio.h>
#include <stdlib.h>

#define SIZE (10)

typedef struct _debugLab
{
    int i;
    char c;
} debugLab;


// Prototypes
void PrintUsage(char *);
void DebugOption1(void);
void DebugOption2(void);

int main(int argc, char **argv)
{
    int option = 0;

    if (argc == 1)
    {
        PrintUsage(argv[0]);
        exit(0);
    }

    option = atoi(argv[1]);

    if (option == 1)
    {
        DebugOption1();
    }
    else if (option == 2)
    {
        DebugOption2();
    }
    else
    {
        PrintUsage(argv[0]);
        exit(0);
    }
}

void PrintUsage(char *command)
{
        fprintf(stderr, "Usage: %s [1 2]\n", command);
        fprintf(stderr, "           1 = Mode 1\n");
        fprintf(stderr, "           2 = Mode 2\n");
}

void DebugOption1()
{
    int i, j;
    int sum = 0;
    char charArray[SIZE];

    fprintf(stdout, "Here we are in DebugOption1()\n");

    
    for (i = 0; i < SIZE; i++)
    {
///////////////////// THE LINE BELOW HAS A BUG ////// CORRECTION BELOW////
///////////  sum += sum + i;///////////
             sum = sum + i;
    }

    fprintf(stdout, "The sum of integers from 0 to %d is: %d\n", SIZE, sum);

}

void DebugOption2()
{
    debugLab *dl1 = NULL;
    debugLab *dl2 = malloc(sizeof(debugLab)); // Always check for a NULL Pointer after a call to malloc
    debugLab dl3;

    dl3.i = 37;
    dl3.c = 'A';
    dl2->i = 36;
    dl2->c = 'B';
    dl1->i =35;   // Oops! dl1 is NULL!
    dl1->c = 'C';

}

====================================================

Add a comment
Know the answer?
Add Answer to:
Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include...

    devmem2.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <termios.h> #include <sys/types.h> #include <sys/mman.h>    #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { int fd; void *map_base = NULL, *virt_addr = NULL; unsigned long read_result, writeval; off_t target; int access_type = 'w';    if(argc...

  • C Language Programming. Using the program below - When executing on the command line only this...

    C Language Programming. Using the program below - When executing on the command line only this program name, the program will accept keyboard input and display such until the user does control+break to exit the program. The new code should within only this case situation “if (argc == 1){ /* no args; copy standard input */” Replace line #20 “filecopy(stdin, stdout);” with new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

  • #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc...

    #include <stdlib.h> #include <stdio.h> #include "main.h" #define MAX_NUM_LENGTH 11 void usage(int argc, char** argv) { if(argc < 4) { fprintf(stderr, "usage: %s <input file 1> <input file 2> <output file>\n", argv[0]); exit(EXIT_FAILURE); } } /* This function takes in the two input file names (stored in argv) and determines the number of integers in each file. If the two files both have N integers, return N, otherwise return -1. If one or both of the files do not exist, it...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n)...

    so in this code, it computes the sum 1+2+....+n but i want it to compute 2*(1+2+....+n) using semaphores implement solution to the critical section problem #include #include int sum; /* this data is shared by the thread(s) */ void *runner(void *param); /* threads call this function */ int main(int argc, char *argv[]) { pthread_t tid; /* the thread identifier */ pthread_attr_t attr; /* set of thread attributes */ if (argc != 2) { fprintf(stderr,"usage: a.out \n"); return -1; } if...

  • Below is a basic implementation of the Linux command "cat". This command is used to print...

    Below is a basic implementation of the Linux command "cat". This command is used to print the contents of a file on the console/terminal window. #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) {FILE *fp; if(2 != argc) {priritf ("Usage: cat <filename>\n"); exit(1);} if ((fp = fopen(argv[1], "r")) == NULL) {fprintf (stderr, "Can't. open input file %s\n", argv[1]); exit (1);} char buffer[256]; while (fgets(X, 256, fp) != NULL) fprintf(Y, "%s", buffer); fclose(Z); return 0;} Which one of the following...

  • Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...

    Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd );   pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be...

    Read given code RaceOrNot3.c and write all possible outputs of the program. Assume there will be no thread creation or joining failures or semaphore failures. If you believe there is only one possible output, you just need to write that output. #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> sem_t s1; int c=0,x=0; void *UpdateC1(void *arg) {    int i;    for(i=0;i<2000000;i++)   {        sem_wait(&s1);        c++;   x++;        sem_post(&s1);    } } void *UpdateC2(void *arg) {    int i,x=0;    for(i=0;i<2999999;i++)   {        sem_wait(&s1);        c++;   x++;       ...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT