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
  • Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the...

    Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested for file open.” Append to the program to output to the text log file a new line starting with day time date followed by the message "SUCCESSFUL". Append that message to a file “7Error_Log_File.txt” . ?newline Remember to be using fprintf using stderr using return using exit statements. Test for file existence, test 7NoInputFileResponse.txt file not null (if null...

  • 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...

  • can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h>...

    can someone help me with changing this to c++ language #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <string.h> #include <dirent.h> #include <sys/wait.h> #include <time.h> #include <sys/stat.h> #include <unistd.h> char *pathLog; char *pathRep;    struct stat attr; int fileNum, curNum; char *create_logPath(char *directory);    void *funcChecker(void *pathSub); void *funcprintTimeAndChanges(void *pathSub);    void main(int argc, char *argv[]) {    if(argc < 3)    { printf("%s must be executed with exactly two additional arguments (pathRep, pathSub)!\n", argv[0]); printf("Typed count is %d\n", argc); printf("Aborted...

  • Modify When executing on the command line having only this program name, the program will accept...

    Modify When executing on the command line having 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 your new code. Open read a text file “7NoInputFileResponse.txt” that contains a message “There were no arguments on the command line to be tested...

  • #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...

  • Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE...

    Consider the following, partially-complete, C-program: #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #define BUFFERSIZE 100 #define COPYMODE 0644 main(int argC, char* argv[]) {    int srcFd;    int dstFd;    int charCnt;    char buf[BUFFERSIZE];    /*Check args*/    if( argC!=3 ){        fprintf( stderr, "usage: %s source destination\n", argv[0]);        exit(1);    }    /*Open the files*/       srcFd= open(argv[1],O_RDONLY);    if( srcFd==-1 ){        fprintf(stderr,"Cannot open %s \n", argv[1]);    }    dstFd= creat(argv[2],COPYMODE);   ...

  • #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage:...

    #include <errno.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* * Expected usage: * ./wc <words | lines> <file> * * If argv[1] is "words", then you should count the number of words. If it * is "lines", then you should count the number of lines. * * For example: * $ cat a.txt * a b c d * $ ./wc words a.txt * 4 * $ ./wc lines a.txt * 1 * * YOUR PROGRAM...

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