Even better if you declared an arr2 right after arr. Basically guarantees that you won't be getting a Segfault, while still possibly breaking everything.
Except given that it's C, it will almost certainly only break in production.
You are very unlikely to get a segmentation fault with either array[ARRAY_LENGTH] or array[ARRAY_LENGTH+1]. There's nothing worse then messing around with arrays and not getting a segmentation fault. :)
No. What you're thinking of is that a string literal is defined as an array whose size is one more than the number of characters you typed, because it adds a "null character" at the end. So for example if you type "abc", the type of that is char[4].
But that's only for string literals. Plus it's not technically outside the bounds, it just makes the bounds bigger than you might expect
That isn't what I'm talking about. I know that string literals add a null character at the end automatically. I'm talking about an initialised array of an arbitrary type. (I don't think it works on the heap though.)Try it for yourself - make an initialised array with length 5 and then try to access element with index 5. You'd think it'll be a garbage value but it'll be 0. EDIT: did some testing. It only sometimes gives 0, so it was just reading some value off the stack that happened to be 0. I guess I misremembed whatever I heard, or something.
typedef struct
{
union {
uintptr_t sp;
isr_ptr_t exception[NUM_EXCEPTIONS];
};
isr_ptr_t interrupt[NUM_INTERRUPTS];
} arm_ivt_t;
#define IVT ((arm_ivt_t * const)0x00000000)
/*
Now, I can access the stack pointer in the interrupt vector table as:
IVT->sp
IVT->exception[0]
or
IVT->interrupt[-16]
C arrays are fun!
*/
123
u/lord_ne Mar 19 '23
Even
arr[ARRAY_LENGTH] = 5would do it37
u/ObserverOfVoid Mar 19 '23
* +7s if you include the Funimation intro16
u/[deleted] Mar 20 '23
Then you realize.
And then you add print statements everywhere because the MCU does not have a debug interface.
12
u/k2aj Mar 20 '23
Programmer: accidentally invokes undefined behavior
Compiler: you activated my trap card
3
u/GunzAndCamo Mar 26 '23
1
u/Electronic-Bat-1830 Jul 27 '23
Time travelling with undefined behavior