Exc_Bad_Instruction Error

Exception: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) using C++

vector<int> printMatrix(vector<vector<int> > matrix) {
if (matrix.empty()) {
return vector<int>();
}
this->matrix = std::move(matrix);
int startRow = 0, lastRow = this->matrix.size() - 1;
int startCol = 0, lastCol = this->matrix[0].size() - 1;
while (startRow <= lastRow && startCol <= lastCol) {
printCircle(startCol, lastCol, startRow, lastRow);
++startCol, --lastCol, ++startRow, --lastRow;
}
// **** return a vector here ****
}

Need to return a vector from the function, or change it to be void.

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) - underlying causes

The identifier "EXC_BAD_INSTRUCTION" seems to indicate an attempt to execute an invalid opcode. On IA32 hardware, it is a fault of type Invalid Opcode Exception (#UD) and has interrupt vector value of 6. It can be triggered for a number of reasons but it generally means that the byte sequence that encode the incoming instruction is invalid or reserved, or has inconsistent operands relative to the instruction, or is to long (>15 bytes), or uses an IA32 extension not supported by the current hardware, or is the UD2 opcode, or attempting to execute some instruction while the machine is in some state that prevents its execution, or some other corner cases.

Now, one possible explanation for cause for this kind of fault is that the compiler you are using assumes that some hardware features are accessible to the target (executing) machine and compiles code accordingly. The target machine features can generally specified as compile flag options. For example, floating point operations and standard math functions will normally only generate x87 fpu instruction, but using the combination of -mfpmath=sse and -msse instruct compiler to generate SSE scalar instructions for usual floating point calculations. The SEE instruction sets are an extension feature of IA32 architecture and are not available on all machines. Portable code for an architecture should be compiled to machine independent generic code for this architecture.

Another possible but less probable cause for this hardware fault is that the compiler might itself be bugged and generate some invalid byte sequence.

I don't think some undefined behavior would lead to an invalid opcode fault on most architectures. While it is true that UB may lead to any kind of erratic behavior, UB conditions are mostly never detectable at compile time and as such, the usually generate a General Protection Fault (#GP) on IA32 (which is called a segmentation fault in unix nomenclature). On the opposite, attempting to generate an undefined opcode is a condition that is always detectable at compile time (unless the code is self generating at runtime, or if the byte code stream gets misaligned and the instruction pointer points in the middle of some opcode).

Why do I get the EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in swift

for .. in .. in Swift does not iterate over indices. It iterates over actual array elements. Hence i is the element, not the index.

Using if i % 2 == 0 will fix your problem.

SwiftUI :Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

In the context of Swift code,

EXC_BAD_INSTRUCTION

usually means you’ve hit a compiler trap, that is, an undefined instruction inserted into the code by the compiler because of a bug detected at runtime. The most common cause of these are:
failure to unwrap an optional —

  1. This can be a forced unwrap (!) or an implicit unwrap (accessing an implicitly unwrapped optional that’s nil).
  2. array out of bounds
  3. a failed forced cast (as!), either because the value was a nil optional or because the value was of the wrong type

You can debug this issue by creating a exception breakpoint. As the name suggests, this stops the code execution before the execution of the line that throwed this exception.

To Create a exception breakpoint in Xcode, Go to BreakPoint navigator -> Click the + icon at the bottom left corner -> Select Exception Breakpoint.

More about breakpoints check this link



Related Topics



Leave a reply



Submit