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

Instruction struct has an int named address, and a char* named info and info wil

ID: 3662411 • Letter: I

Question

Instruction struct has an int named address, and a char* named info and info will hold the address of a dynamically allocated array of char sized to fit the information stored. Reader struct holds the information read from the file. It stores each line along with its simulated memory address. Reader has an array of 1000 Instruction named “lines” that holds each line of the file that takes up simulated memory. Only assembly language instructions use simulated memory space. As you read in each line of the file, you should allocate room for it in the lines[pos].info array, and store its simulated memory address. The program starts at address 100. I'm having trouble storing the lines, here's my code so far:

struct Reader //stores info read from file stores
     //each line along with its simulated memory address
{
Instruction lines[1000]; //only for assembly instructions
};
struct Instruction
{
int address;
char* info; // = new char[int];holds dynamic address of info stored

};

void read(Reader* r, char filename[] )//------delete----//
{
//ifstream fin(argv[1]); //----uncomment---//
ifstream fin;//----delete----//
fin.open(filename);
int index = 0;

while (!fin.eof())
{
  const int SIZE = 100;
  char line[SIZE];
  char* str = nullptr;


  fin.getline(line, SIZE);
  
  str = strstr(line, "main:");
  if(str)
   continue;
  if (line[0] == '.' || line[1] == '.')
   continue;

  for (size_t i = 0; i < strlen(line); i++)
  {

   if (line[i] == ' ' || line[i] == ' ')
    line[i] = ' ';
   
  }

  r->lines[index].info = new char[index]; //this is the part I'm having trouble with
  strcat(r->lines[index].info, line);
  index++;


   }
fin.close(); //closing file
} //read()

Explanation / Answer

You declared " r->lines[index].info = new char[index];"

Where new char[index] so problem arises here..

Declare it   r->lines[index].info = new char[int];

It will works fine.