Question: What is the proximate error, and where in the source code does it occur? What is the root error, and where in the source code
- What is the proximate error, and where in the source code does it occur?
- What is the root error, and where in the source code does it occur?
- What indications of an error are displayed in the program's presentation?
- What indications of an error are displayed in the program's valgrind profile?
- What is the sequence of gdb commands you used to identify the location of the root and proximate errors?
- What fix would you suggest for this bug?
#pragma once #include
int strlen(const char *); // User-defined strlen function int strcmp(const char *, const char *); // User-defined strcmp function int main(int, char **, char **); // Standard main function
int main(int argc, char **argv, char **envp) { // List Control int envc = 0; // Length of envp array int size = 0; // Number of elements stored in our list for (envc = 0; envp[envc] != nullptr; envc++); // Count number of environment variables int capacity = argc + envc; // Size of list needed (command line args + environment vars) char * list[capacity]; // The list bool found = false; // List search result int position = 0; // List insertion position
// Add command line arguments to the list: ordered, no duplicates for (auto idx = 0; idx < argc; idx++) { found = false; // Reset search result for(position = 0; position < size; position++) { if (strcmp(list[position],argv[idx]) == 0) { found = true; break; } if (strcmp(list[position], argv[idx]) > 0) { found = false; break; } } if (found == false) { for (auto ptr = size; ptr > position; ptr--) list[ptr] = list[ptr - 1];
list[position] = argv[idx]; size++; } }
// Add environment variables to the list: ordered, no duplicates for (auto idx = 0; idx < envc; idx++) { found = false; // Reset search result for(position = 0; position < size; position++) { if (strcmp(list[position],envp[idx]) == 0) { found = true; break; } if (strcmp(list[position], envp[idx]) > 0) { found = false; break; } } if (found == false) { for (auto ptr = size; ptr > position; ptr--) list[ptr] = list[ptr - 1]; list[position] = envp[idx]; size++; } }
// Display the list std::cout << "List: " << std::endl
; for(auto idx = 0; idx < size; idx++) { std::cout << list[idx] << std::endl ; }
return 0; }
/* Purpose: Calculate and return length of C-String Parameters const char * string is the pointer to the C-string. The 'const' is needed so we can accept argv and envp strings Return: int length of c-string (not counting the null terminator) */ int strlen(const char * string) { int result = 0; // Result for(result = 0; string[result] != '\0'; result++);// Counting loop return result; // Return the count } /* Purpose: Compare C-strings Parameters const char * left, const char * right strings to compare Return: int == 0 means the strings are identical < 0 means left < right > 0 menas left > right */ int strcmp(const char * left, const char * right) { for ( ; *left == *right; left++, right++) if (*left == '\0') return 0; return ((*(unsigned char *)left < *(unsigned char *)right) ? -1 : +1); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
