0% found this document useful (0 votes)
23 views4 pages

Debugging Array Issues in C Code

The document outlines several coding problems related to array handling in C, including indexing mistakes, memory overwrite issues, pointer arithmetic errors, and improper array allocation. Each problem includes a code snippet and a task to identify and fix the errors. The document serves as a practical exercise for students in the Computer Science and Engineering department at Dr. Sivanti Aditanar College of Engineering.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

Debugging Array Issues in C Code

The document outlines several coding problems related to array handling in C, including indexing mistakes, memory overwrite issues, pointer arithmetic errors, and improper array allocation. Each problem includes a code snippet and a task to identify and fix the errors. The document serves as a practical exercise for students in the Computer Science and Engineering department at Dr. Sivanti Aditanar College of Engineering.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Dr.

SIVANTHI ADITANAR COLLEGE OF ENGINEERING – TIRUCHENDUR


DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
&
COMPUTER SOCIETY OF INDIA
SACOSIUM -2025
Coding Desenvolvimento

1. Array Indexing Mistake

Problem: You are given the following code that attempts to calculate the sum of the
elements in an array, but there’s an indexing bug.

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int sum = 0;

for (int i = 0; i <= sizeof(arr)/sizeof(arr[0]); i++) {


sum += arr[i];
}

printf("Sum of array elements: %d\n", sum);


return 0;
}

Task: Identify and fix the bug in the code.

2. Memory Overwrite with Arrays

Problem: The following code snippet aims to copy an array of integers into another array.
However, due to improper handling of the array size, it causes a memory overwrite issue.

#include <stdio.h>
#include <string.h>

void copy_array(int* source, int* dest, int n) {


for (int i = 0; i < n; i++) {
dest[i] = source[i];
}
}

int main() {
int source[] = {10, 20, 30, 40, 50};
int dest[4];
copy_array(source, dest, 5);

for (int i = 0; i < 4; i++) {


printf("%d ", dest[i]);
}
printf("\n");

return 0;
}

Task: Find and fix the issue that causes an out-of-bounds write and potential memory
corruption.

3. Pointer Arithmetic Error

Problem: The following program attempts to reverse an array using pointer arithmetic.
However, due to a logical error, the reversal does not occur correctly.

#include <stdio.h>

void reverse_array(int *arr, int n) {


int *start = arr;
int *end = arr + n - 1;

while (start < end) {


int temp = *start;
*start = *end;
*end = temp;

start++;
end--;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = 5;

reverse_array(arr, n);

for (int i = 0; i < n; i++) {


printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

Task: Find the logical error in the array reversal function and correct it.

4. Array Allocation and Pointer Dereferencing Issue


Problem: In the following code, memory for the array arr is dynamically allocated.
However, there's a mistake in how the array elements are accessed, leading to undefined
behavior.

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

int main() {
int *arr;
int size = 5;

arr = (int*) malloc(size * sizeof(int));

for (int i = 1; i < size; i++) { // Bug: starts from index 1 instead
of 0
arr[i] = i * 10;
}

for (int i = 0; i < size; i++) {


printf("%d ", arr[i]);
}
printf("\n");

free(arr);

return 0;
}

Task: Identify the mistake in array indexing and suggest the correct approach to fix the code.

5. Array Bounds with Negative Indexing

Problem: The following code is supposed to find the maximum and minimum values in an
array. However, it contains an error that leads to undefined behavior due to invalid indexing.

#include <stdio.h>

void find_max_min(int arr[], int n) {


int max = arr[0];
int min = arr[0];

for (int i = -1; i < n; i++) { // Bug: invalid starting index


if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}

printf("Max: %d, Min: %d\n", max, min);


}

int main() {
int arr[] = {4, 9, 2, 7, 5};
int n = sizeof(arr) / sizeof(arr[0]);

find_max_min(arr, n);

return 0;
}

Task: Find the issue with array indexing in the find_max_min function and fix it.

Common questions

Powered by AI

The mistake in the dynamic array indexing is starting from index 1 instead of 0 when assigning values to 'arr'. This leaves 'arr[0]' uninitialized, leading to undefined behavior when accessed. To fix it, initialize the loop counter from 0, i.e., 'for (int i = 0; i < size; i++)', so all elements are correctly initialized .

The memory overwrite problem arises because the destination array 'dest' is smaller than the source array 'source', and the function attempts to copy more elements than 'dest' can hold. To fix this, either ensure the destination array is of the same size as the source array or modify the 'copy_array' function to only copy up to the size of the destination array, i.e., replace 'n' with the size of 'dest' in the 'copy_array' call .

The find_max_min function incorrectly starts its loop from index -1, which is an invalid position for an array, leading to undefined behavior. To fix this, change the initial index of the loop from '-1' to '0', ensuring all indices are valid within the bounds of the array .

The logical error in the array reversal function is not with the pointer arithmetic itself, but ensuring that the reversal process actually happens. However, upon reviewing the code, there does not seem to be an error in the given 'reverse_array' function; the logic is correct as 'start' moves from beginning towards the center and 'end' moves from end towards the center, swapping elements. No change is needed .

The indexing bug in the code occurs because the loop condition uses '<=' instead of '<'. This causes the loop to access an element out of bounds. To fix the bug, change the loop condition to 'i < sizeof(arr)/sizeof(arr[0])'. This ensures that the loop iterates only through valid indices of the array .

You might also like