Elan
Elan

Reputation: 549

C control flow, memory allocation: Abort trap 6, populate array with characters in string, nested for loops

This is a question about control flow and possibly memory allocation.

Given a long long number, I am doing the following:

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

int main(void)
{
    long long n = 12345678; // I am given a number

    char str[8]; // initialize string of length 8
    sprintf(str, "%2lld", n); // convert n to string
    printf("The string is: %s\n", str); // check that n is converted to string

    int arr[4]; // initialize array of length 4

    for (int i = 6; i >= 0; i -= 2) // select every other char in string, starting from second-to-last char
        {
            for (int j = 0; j < 4; j++) // select position of array
            {       
                arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
                printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
            }
        }

    for (int k = 0; k < 4; k++) // print contents of array
    {
        printf("The product at position %d is %d\n", k, arr[k]);
    }
    return 0;
}

There are two problems with this code:

  1. Generates Abort trap: 6 error when run on MacOS Terminal.
  2. When the code executes successfully in a practice IDE environment, it generates the following result:

The string is: 12345678
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The digit is 1 and the product is 2
The product at position 0 is 2
The product at position 1 is 2
The product at position 2 is 2
The product at position 3 is 2

What I want is to resolve the Abort trap: 6 error and to get the following result:

The string is: 12345678
The digit is 7 and the product is 14
The digit is 5 and the product is 10
The digit is 3 and the product is 6
The digit is 1 and the product is 2
The product at position 0 is 14
The product at position 1 is 10
The product at position 2 is 6
The product at position 3 is 2

What should I change in the code to achieve that?

I have read other posts about Abort trap: 6 and I don't know how this code is committing a mistake in memory allocation/usage.

Upvotes: 2

Views: 62

Answers (2)

dbush
dbush

Reputation: 224072

Your str array isn't long enough:

char str[8]; // initialize string of length 8

To hold a string of 8 characters, you need an array of 9 bytes: 8 for the characters in question, and one for the terminating null byte.

Because you don't put aside enough space for the string, you end up writing past the end of the array. Doing so invokes undefined behavior, which in this case manifests in a crash.

Increase the size of your array so you don't step on invalid memory:

char str[9]; 

As for the output you're seeing, you're overwriting the value of arr[j] on each iteration of the outer loop. Rather than a nested loop, have a single loop that iterates on both i and j.

Upvotes: 0

kocica
kocica

Reputation: 6467

You dont need for loop to increment j as an array index, just set is to 0 at the beginning and with every loop increment it.

for (int i = 6, j = 0; i >= 0; i -= 2, j++) // select every other char in string, starting from second-to-last char
{
    arr[j] = (str[i] - '0') * 2; // convert char to int, multiply by 2, and assign to array position
    printf("The digit is %c and the product is %d\n", str[i], arr[j]); // announce each entry to the array
}

As @Keine Lust mentioned in comments, you have to alloc space for null terminating character.

char str[8];

change to

char str[9];

Upvotes: 1

Related Questions