0

I'm working on creating a simulator for RISC.

Below are a different part of codes relevant to my question.

main.cpp

memory* main_memory; processor* cpu; main_memory = new memory (verbose); //memory classs implemented correctly cpu = new processor (main_memory, verbose); interpret_commands(main_memory, cpu);// takes to commands class 

In processor.cpp, I have to access a function of memory class that is public.

memory.cpp:

 uint32_t memory::read_word (uint32_t address) { // some calculation and returns a hex word at address } 

processor.cpp:

#include "memory.h" void processor::execute(unsigned int num, bool breakpoint_check){ if(some condition){ //call read_word at address } } 

I do have a parameterized constructor in processor.cpp that has memory class pointer:

processor::processor(memory* main_memory, bool verbose){ } 

The question is, how can I call read_word function(of class memory) from the execute function (of class processor)?
Any help would be highly appreciated :)

9
  • 1
    You forgot to ask a question. Commented Jun 16, 2019 at 2:05
  • Here's the thing about bugs. If you don't know what they are, how do you know what is the relevant code? Heck, what is the relevant problem? Commented Jun 16, 2019 at 2:05
  • Side note: you probably don't need any dynamic allocations, and if you do, avoid new. Use a smart pointer. Commented Jun 16, 2019 at 2:09
  • lol @JohnZwinck. Sorry have been working on simulator whole night :) Updated it now. Commented Jun 16, 2019 at 2:10
  • @user4581301, I updated the question. I need to access read_word() from execute (). Commented Jun 16, 2019 at 2:11

1 Answer 1

1

You just need to store the main_memory* in the constructor as a member variable, so you can use it later in another member function:

processor::processor(memory* main_memory, bool verbose) : _main_memory(main_memory) {} void processor::execute(unsigned int num, bool breakpoint_check){ if(some condition){ _main_memory->read_word(...); } } 

And add memory* _main_memory = nullptr inside class processor.

Sign up to request clarification or add additional context in comments.

4 Comments

What's my_address here? Is it like address of current instruction as in pc? Cause the constructor is not getting any address argument.
What's the _main_memory(main_memory)?
_main_memory(main_memory) is an initialization list. It's the usual way to initialize member variables, though if you prefer you can just say _main_memory = main_memory; inside the constructor body.
Oh, I got it. It was supposed to be memory* _main_memory= nullptr in the class processor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.