Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

if(number_of_instruction==size) { cout<<\"Number of instruction: \"< Instruction

ID: 3785488 • Letter: I

Question

if(number_of_instruction==size)

{

cout<<"Number of instruction: "<

Instruction** old_ins= new Instruction*[size];

for(int i=0;i

{

old_ins[i] = new Instruction();

old_ins[i]->setMetaDataCode(ins[i]->getMetaDataCode());

old_ins[i]->setMetaDataDescriptor(ins[i]->getMetaDataDescriptor());

old_ins[i]->setNumberOfCycles(ins[i]->getNumberOfCycles());

}

int old_size=size;

size=old_size*2;

Instruction** ins= new Instruction*[size];

for(int i=0;i

{

ins[i] = new Instruction();

}

for(int i=0;i

{

ins[i]->setMetaDataCode(old_ins[i]->getMetaDataCode());

ins[i]->setMetaDataDescriptor(old_ins[i]->getMetaDataDescriptor());

ins[i]->setNumberOfCycles(old_ins[i]->getNumberOfCycles());

}

delete []old_ins;

for (int i=0; i

{

old_ins[i]=nullptr;

}

cout<<"Number of instruction: "<

ins[number_of_instruction]->setMetaDataCode("P");

}

cout<<"qweqweqweqwe"<

if(number_of_instruction==100)

{

cout<<"Number of instruction: "<

ins[number_of_instruction]->setMetaDataCode("P");

cout<<"Stop here"<

cout<getMetaDataCode()<<"~~~~~~"<

}

I tried to write the code of resizable array of objects in c++, but the weird thing is that I called "ins[number_of_instruction]->setMetaDataCode("P")"; twice in these two if statement. I got the erroe message, whcih is that "Segmentation fault" in the second if statement. The test result is:

Number of instruction: 100
Number of instruction: 100
qweqweqweqwe
Number of instruction: 100
Segmentation fault (core dumped)

Does anyone know why? How can I fix it?

Explanation / Answer

You don't have an ins outside your first if block. PLease define Instruction** ins= new Instruction*[size];

outside your if block.

Also please share full code for detailed look this code has missing piece all over.