+1 (315) 557-6473 

Code Debugging Assignment Solution in C


C language code debugging

Write a C assignment Test these codes, explain your answer.

1. consider the following program

int main(int argc, char* argv[]){ FILE* fp = fopen(argv[1],"r"); return 0; }

   Compile the code: gcc main.c

   Run the code: ./a.out

   Will throw a segmentation fault.

   Use debugger to figure out where the errors are

Solution:

Upon debugging the code, a segmentation fault occurs at this line of code:

FILE* fp = fopen(argv[1],"r");

The error is pointing specifically at "argv[1]". If we run the program using "./a.out", we are not passing any additional arguments. This, therefore, means that "argv" will have no element found at index 1. The segmentation fault means that we are accessing an index in an array that does not exist.

2. Write this code to a program and see what happens.

int* ptr = malloc(4); free(ptr); scanf("%d",*ptr);

   use the GDB to find the error

Solution:

Upon debugging the code, error occurs at "scanf("%d", *ptr);". The pointer has been freed/deallocated before the scanf operation has occurred. This means that the pointer is now unusable. Operating an unusable pointer will lead to an error.

3. The following code is supposed to swap two strings (char*'s)

      What can go wrong with the code? Assume A[i] and A[j] are strings with pre-allocated memory to hold strings s1 and s2.

char tmp[50]; strcpy(tmp,A[i]); strcpy(A[i],A[j]); strcpy(A[j], tmp);

Solution:

There are 2 things that can go wrong here:

  1. We are assuming that character arrays A and B have a size of at most 50 characters. This is because the tmp array can only hold up to 50 characters. If we copy a string array of more than 50 characters and put it into an array with less than 50 characters then it will be truncated. After the swap, the data would end up inaccurate.
  2. A string character array is usually ended with a NULL terminator or the symbol '\0' to indicate that it is the end of a string. It is possible that character arrays A and/or B might not have that terminator in them and doing a strcpy operation will lead to unexpected results.

4. What are some of the differences between char A[50] and char* A = malloc(50)?

Solution:

A[50] is a fixed-size static array. This means that when the program runs, the operating system automatically reserves already a memory that can hold 50 items. There is no other way we can modify the reserved size all throughout while the program is running. After the program terminates, the operating system automatically reclaims the memory space that has been used.

"char* A = malloc(50)" is a dynamic allocation of the array. When the program runs, "char *A" is just an empty pointer and we can dynamically tell the operating system to reserve a memory using the "malloc" operation. Afterwhich, we use a pointer such as "char *A" to point on that reserved memory so we can use it. While the program is running, we can reclaim that reserved memory using the "free" operation, and/or we can also reserve another set of memory spaces of another size if needed. Dynamically allocated memory is not automatically reclaimed by the operating system when the program stops. It is a practice for C developers to "free" them before terminating the program. If we do not "free"/reclaim the memory and the program is terminated already, then that memory space is going to be a waste because other programs won't be able to use that memory space. The operating system thinks that it is still being used by another program.

It is convenient to use a fixed-size static array because memory management is done by the operating system. The downside is that we cannot increase the size when needed. It is also never been a good idea to reserve a large size of the static array because the operating system’s memory is limited. Moreover, it would be a waste of space if only a few of the memory space of the reserved memory is being utilized. This downside can be avoided if we go for a dynamic array so that we can reserve the exact size we need and/or shrink/expand if needed.