3

I am a beginner to C and am trying to get more familiar with arrays and the concept of manual memory allocation by doing simple exercises. I have been reading all the (many) questions on SO regarding the "Abort trap: 6" error, and though I've learned a lot, they haven't solved my issue.

Similar threads I checked out include:

"Abort trap: 6" running C program on a Mac

"Abort trap: 6" error in C?

...and more, all slightly different than what I'm dealing with.

The problem seems to be that I'm writing to memory I don't have access to, but I thought that by making the array big enough when I declare it, I would avoid this issue. Evidently I was wrong!

The code is supposed to simply create an array that holds 100 ints (in positions 0 to 99), and assign each one the value of its position (i.e. the first item in the array should be the int 0, and the last should be the int 99). When I run this code, I get all the example printf statements as expected – with the correct values in them – but it's followed by a line saying "Abort trap: 6".

Could someone take a look at my code and tell me what I'm doing wrong to cause this error?

#include <stdio.h>


int main(void)
{
    int obvs[101];

    for (int i = 0; i < sizeof(obvs); i++)
    {
        obvs[i] = i;
    }

    printf("obvs[9] = %i\n", obvs[9]);
    printf("obvs[13] = %i\n", obvs[13]);
    printf("obvs[37] = %i\n", obvs[37]);
    printf("obvs[74] = %i\n", obvs[74]);
    printf("obvs[99] = %i\n", obvs[99]);

    return 0;
}
ChiselD
  • 61
  • 1
  • 7
  • `i < sizeof(obvs)` --> `i < sizeof(obvs)/sizeof(obvs[0])` (Also `int obvs[101];` --> `int obvs[100];`) – BLUEPIXY Aug 17 '17 at 03:03
  • Sizeof ovs is 4*101 bytes. – MCG Aug 17 '17 at 03:06
  • Thank you both for the comments! My beginner-mind needed a moment to wrap itself around them, but then I realized the problem was that I thought I knew what the size of an int was when I clearly didn't. Haha, noob. :) Again, thanks y'all. – ChiselD Aug 17 '17 at 03:16

2 Answers2

2

Your problem is your for loop:

for (int i = 0; i < sizeof(obvs); i++)

More specifically, your porblem is your terminating condition:

i < sizeof(obvs)

As your code is written, sizeof(obvs) is going to return the size of the memory allocated (in this case, 404 bytes since an int requires 4 bytes of memory) to your array not the size of your array, 101, like you're probably expecting.

Change your for loop to read:

for (int i = 0; i < 101; i++)

or

for (int i = 0; i < (sizeof(obvs) / sizeof(int)); i++)

And it should fix your problem. A good habit to get into is to store constant values in macros so that you're guaranteed to use the same value each time it's used (and save yourself some headache).

So you could rewrite your code to read:

#include <stdio.h>

#define ARRAY_SIZE 101

int main(void)
{
    int obvs[ARRAY_SIZE];

    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        obvs[i] = i;
    }

    printf("obvs[9] = %i\n", obvs[9]);
    printf("obvs[13] = %i\n", obvs[13]);
    printf("obvs[37] = %i\n", obvs[37]);
    printf("obvs[74] = %i\n", obvs[74]);
    printf("obvs[99] = %i\n", obvs[99]);

    return 0;
}
Mike
  • 3,830
  • 2
  • 16
  • 23
  • Many thanks, Mike! I appreciate the detailed explanation – it helps a lot for someone who's still at a really basic level, like me. – ChiselD Aug 17 '17 at 03:28
  • @ChiseID You're welcome! It's also good practice to use `i < sizeof(obvs)/sizeof(obvs[0])` or `i < sizeof(obvs)/sizeof(int)` as suggested in other answers. What that snippet does is take the size of your array, divide it by the size of each element in the array and thus returns the total elements in the array. I'd argue that's typically a better solution but if you're allocating a new array and you know that the array is always going to have the same size it's really good practice to have that size defined in a macro keep your code readable and get a better flavour for what the array does. – Mike Aug 17 '17 at 03:32
1

The behavior is UB as you are filling array outside of its size.When you are filling value in the array you are filling 101*4 elements. As it is integer array each element will take 4 bytes. In this case sizeof(int) is 4. So you have to divide sizeof(ovs) with the size of an element of the array. Therefore change i < sizeof(obvs); to i < sizeof(obvs)/sizeof(obvs[0]);

MCG
  • 1,011
  • 1
  • 10
  • 21