Thread: Array of structures in a class, init

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    Array of structures in a class, init

    Hi,

    Is there any simpler way of initiating an array used in a class than looping or single instance init.
    I want to do EXACTLY as the code states but I don't get the damn thing working.

    Code:
    typedef void InstructionProc(uint32_t icode);
    
    typedef struct Instruction {
            uint32_t mask;
            uint32_t value;
            char *name;
            InstructionProc *proc;
    } Instruction;
    
    
    class test {
    Instruction instr[];
    }
    
    
    
    test::test() {
    
    instr[] = {
    	{0x0de00000,0x00a00000,"adc"    ,i_adc}
    };
    
    }
    /S

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    In C++, structs are almost exactly the same as classes (default members and access is public, classes default is private). This means you can have a clever little struct constructor. This means you could have something like:

    Code:
    typedef void InstructionProc(uint32_t icode);
    
    void i_adc(uint32_t meh) 
    { 
    	// We're here
    }
    struct Instruction {
    	uint32_t mask;
    	uint32_t value;
    	char *name;
    	InstructionProc *proc;
    
    
    	Instruction(uint32_t mask, uint32_t value, char* name, InstructionProc proc)
    	{
    		this->mask = mask;
    		this->value = value;
    		this->name = name;
    		this->proc = proc;
    	}
    	Instruction()
    	{
    		// default values here?
    	}
    };
    
    class Test
    {
    public:
    	Test()
    	{
    		instructionsA = new Instruction[50];
    		for(int i = 0; i < 50; i++)
    		{
    			instructionsA[i] = Instruction(0x0de00000, 0x00a00000, "adc", i_adc);
    			instructionsB[i] = Instruction(0x0de00000, 0x00a00000, "adc", i_adc);
    			instructionsA[i].proc(i);
    			instructionsB[i].proc(i);
    		}
    	}
    	~Test() 
    	{ 
    		delete instructionsA; 
    	}
    
    private:
    	Instruction* instructionsA;
    	Instruction instructionsB[50];
    };
    Not that I know what this will be doing, doesn't have much direction on it yet really

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In addition to the two potential solutions Tonto gave, you could also use a vector, which can be intialized with the appropriate values instead of requiring a for loop:
    Code:
    #include <vector>
     
    // Assume same Instruction definition as Tonto's code
     
    class Test
    {
    public:
    	Test() : instructionsC(50, Instruction(0x0de00000, 0x00a00000, "adc", i_adc))
    	{ }
    	// Not Needed: ~Test() { }
     
    private:
    	std::vector<Instruction> instructionsC;
    };

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    I want something like this, but instructionA.proc(i) does not evaluate to a function, why?


    Code:
    typedef unsigned int uint32_t;
    
    class Test;
    
    typedef void (Test::*InstructionProc)(uint32_t icode);
    
    
    struct Instruction {
    	uint32_t mask;
    	uint32_t value;
    	char *name;
    	InstructionProc proc;
    
    
    	Instruction(uint32_t mask, uint32_t value, char* name, InstructionProc proc)
    	{
    		this->mask = mask;
    		this->value = value;
    		this->name = name;
    		this->proc = proc;
    	}
    	Instruction()
    	{
    		// default values here?
    	}
    };
    
    class Test
    {
    
    public:
    	Test()
    	{
    		instructionsA = new Instruction[50];
    		for(int i = 0; i < 50; i++)
    		{
    			instructionsA[i] = Instruction(0x0de00000, 0x00a00000, "adc", &Test::i_adc);
    
    		//	instructionsB[i] = Instruction(0x0de00000, 0x00a00000, "adc", i_adc);
    		//	instructionsA[i].proc(i);
    		//	instructionsB[i].proc(i);
    		}
    	}
    	~Test() 
    	{ 
    		delete instructionsA; 
    	}
    
    	void i_adc(uint32_t opcode) {
    	}
    
    	void execute() {
    
    	instructionsA[0].proc(0x02000000);
    		
    	}
    
    private:
    	Instruction* instructionsA;
    	Instruction instructionsB[50];
    
    };
    
    void main() {
    
    
    Test *cpu;
    cpu = new Test();
    
    cpu->execute();
    
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    delete instructionsA;
    That should be
    Code:
    delete[] instruactionsA;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    why should that fix anything...hmmm?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because what you're doing is undefined behaviour. In other words, just plain wrong.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    hmmm....maybe you could correct it then...cause i want to point to a Test memberfunction!!!
    How can I do that?

    Please!!

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I already corrected the specific error I pointed out.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    but i doesn't solve the problem!!!

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by seizmic
    but i doesn't solve the problem!!!
    It shure does solve one of your problems. And there are more. Another one is that there is and has never been void main() in C++ and such crappy code doesn't compile on a decent compiler. It's int main(). ( this solves another problem ).
    And now to what seems to be the problem.
    You have to supply an instance if you want to call a non static classmenberfunction.
    I have rearranged your program and don't know if what is left still does what you want.
    Code:
    #include <iostream>
    using namespace std;
    
    typedef unsigned int uint32_t;
    struct Instruction;
    
    class Test {
    
    public:
    	Test();
    	virtual ~Test();
    
    	void i_adc(uint32_t opcode) {
    	    cout << "i_adc(" << opcode << ")" << endl;
    	}
    	
    	void r_adc(uint32_t opcode) {
    	    cout << "r_adc(" << opcode << ")" << endl;
    	}
    
    	void execute();
    
    private:
    	Instruction* instructionsA;
    };
    
    typedef void (Test::*InstructionProc)(uint32_t icode);
    
    struct Instruction {
    	uint32_t mask;
    	uint32_t value;
    	char *name;
    	InstructionProc proc;
    
    	Instruction(uint32_t mask, uint32_t value, char* name, InstructionProc proc )
    	{
    		this->mask = mask;
    		this->value = value;
    		this->name = name;
    		this->proc = proc;
    	}
    	
    	Instruction() {
    	}
    	
    	void execute(Test & a, uint32_t param ) {
    	      (a.*proc)(param);
    	}
    };
    
    inline Test::Test(){
    	instructionsA = new Instruction[2];
    	instructionsA[0] = Instruction(0x0de00000, 0x00a00000, "adc", &Test::i_adc);
    	instructionsA[1] = Instruction(0x0de00000, 0x00a00000, "adc", &Test::r_adc);
    }
    
    inline Test::~Test() { 
    	delete [] instructionsA; 
    }
    
    inline void Test::execute() {
    	instructionsA[0].execute(*this, 123);	
    	instructionsA[1].execute(*this, 321);	
    }
    
    int main() {
       Test *cpu;
       cpu = new Test();
       cpu->execute();
       return 0;
    }
    Kurt
    Last edited by ZuK; 09-17-2005 at 11:05 AM.

  12. #12
    Registered User
    Join Date
    Aug 2005
    Posts
    27

    Post

    Greatly appreciate your help.....but I have one more dilemma

    I have a function which returns a InstructionProc

    Code:
    inline InstructionProc Test::
    		InstructionProcFind(uint32_t icode) {
            int index=INSTR_INDEX(icode); //just picks out some bits to be used as an index
    	InstructionProc proc=iProcTab[index];
    //iProcTab is an array of InstructionProc's
    
    	return proc;
    }
    then I want to be able to run this like

    Code:
    InstructionProc iproc;
    iproc=InstructionProcFind(1234);
    iproc(1234);
    i know it sounds fishy but i need to get this running in this way, and then I can figure out how to make it smoother!!!

  13. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Same thing you need an instance.
    Code:
    InstructionProc iproc;
    iproc=InstructionProcFind(1234);
     (*this.*iproc)(1234);
    have a look at this

    Kurt

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    27
    greatly appreciate your help....thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of structures
    By tish in forum C Programming
    Replies: 9
    Last Post: 04-05-2009, 03:17 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 2 Dimenstional Array vs Array of Structures
    By jrahhali in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2004, 04:51 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM