Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Value of index randomly changes in dynamic character array during for loop

For one of my assignments in my C book, I have to find a way to treat characters as integers. So, I wrote a program that takes an array of characters that represent integers, add them up, and output the result. In the following program, I attempt to ‘add’ the strings ‘123’ and ‘321’ which should result in ‘444’.

To do this, I first find the distance from ‘3’, ‘2’, and ‘1’ from ‘0’, respectively. For example, ’3’ is 51 in ASCII and ‘0’ 48, so the difference (or distance) is the 3 ASCII code which translates to ETX. Refer to the table below.

ASCII table

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The following is the implementation. Print statements are included for debugging purposes:

#include <stdio.h>

int main (int argc, char *argv[]) {
        char *num1 = "123";
        char *num2 = "321";
        const char Z = 48; //value of zero 
        char from_z = 0;
        char total[] = { 0 };
        int i = 0;
        
        //main for loop
        for (i = 0; num1[i] != '\0' || num2[i] != '\0'; i++) {
                from_z = num2[i] - Z; //distance from '0'
                total[i] = num1[i] + from_z;

                //print out contents of current index and previous
                //to compare
                printf("i total[%d]: %c\n", i, total[i]);
                printf("i - 1: total[%d]: %c\n", i-1, total[i-1]);
        } 
        printf("i: %d\n", i);
        total[i] = '\0';
        
        return 0;
}

The Bug:
When I assign and print the newly assigned index and the previously assigned one, during the second iteration, the value of total[1] changes from ‘4’ to ‘\x01’. Yet, total[0] and total[2] still contain ‘4’. Check the output below. (Of course ‘\x01’ would print out nothing)

i total[0]: 4
i - 1: total[-1]:
i total[1]: 4
i - 1: total[0]: 4
i total[2]: 4
i - 1: total[1]:
i: 3

As you can see, total[1] was first ‘4′, and then randomly became blank. Using the debugger, I could tell that the contents of total[1] is \x01.

What fixed it:

Having a static array where I defined total to have 4 indices like so: char total[4] = { 0 };, the problem was fixed. But I’d still like to know why it doesn’t work for dynamic arrays.

>Solution :

char total[] = { 0 };

is not a dynamic array — it is a fixed-size array with a single element. Writing to any element other than total[0] is writing outside the bounds of the array and invokes undefined behavior.

C doesn’t have "dynamic" arrays that automatically grow as you write new elements to them. If you need a resizable array, you need to allocate memory with either malloc or calloc:

#include <stdlib.h>
...
size_t totalSize = 1;
char *total = calloc( totalSize, sizeof *total ); 

and then manually resize it as necessary using realloc:

if ( i == totalSize )
{
  char *tmp = realloc( total, sizeof *total * (totalSize * 2) );
  if ( tmp )
  {
    total = tmp;
    totalSize *= 2;
  }
}

Make sure to clean up after yourself when you’re done:

free( total );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading