This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problems with c++ classes

Hello,

first of all I would like you to know a little bit about the project I'm doing (can't tell much:).

I am building a system in which I'd like to have a menu implemented as some kind of a finite state machine.

The menu should be able to be nested.

I would like the code for it to be very simple, something like this:

MENU menu;

MENU m1(&menu);
MENU m2(&menu);

MENU m11(&m1);
MENU m21(&m2);
MENU m22(&m2);
MENU m23(&m2);

This code should give me the menu that is nested and looks like this:

MENU---
|      |
M1     M2---
|      |    |
M11    M21  M22

I made a C++ code that works in Visual Studio 2010, but when I tried to migrate it to Keil uVision and put it on a microcontroller, it compiles well, but it blocks before entering the main().

I read that it is generally not a good idea to use dynamic memory allocation in embedded systems, and I don't really need dynamic allocation of memory for the menu, since it will be static, and defined at compiling.

But I would really like to use class approach to it.

To give you another example: i implemented button matrix as a class, so it can easily be defined this way:

        ButtonNetwork BN;

        BN.AddOutLine(LPC_GPIO0, 26);
        BN.AddOutLine(LPC_GPIO0, 25);
        BN.AddOutLine(LPC_GPIO0, 24);

        BN.AddInputLine(LPC_GPIO3, 26);
        BN.AddInputLine(LPC_GPIO3, 25);
        BN.AddInputLine(LPC_GPIO1, 18);
        BN.AddInputLine(LPC_GPIO1, 19);

This way I can define a button matrix very easy with 3 output lines, and 4 input lines.

Also the same problem - the number of lines will be static. I just prefer this approach, because it is easier for later changes to a software.

The microcontroller I use is LPC1768 Cortex M3.
Please tell me what is the easiest way to make it work.

As a temporary solution, I tried increasing the heap size but it doesn't help.

Ill wait for your answers before continuing with this,
Thanks for even reading.

  • Sorry, but there is too little of any question in your post.

    The menues doesn't need any dynamic memory. And there is no problem using C++ to implement them. So there isn't really any open question. If your program hangs, then it's because you have a bug somewhere.

    About the keypad code - it's normally better to have code making use of static declarations than to call methods. Having dynamic mapping of pins like your example indicates means that you have the mapping done at runtime instead of at compile time. That means larger code, and blocks the compiler from doing some of the possible optimizations.

  • Well, I would love to believe into that, but when I try it in Visual Studio C++ it works normally...
    In keil c++ it hangs (it won't even enter the main()). It reports some hard fault condition...

  • Also, can you suggest a way to do the same but with static declarations?

    (to maintain the object oriented code...)

  • It reports some hard fault condition...

    Sorry, but that tells nobody anything. Which hard fault condition, where?

  • Here's the screenshot showing the lines at which it stops, and a foult condition..

    postimage.org/.../

  • Ok guysm I located the problem. Now I'd just like to know how would you handle it.

    Here is the code which does not work.

    The line of code which causes trouble is in red.

    #include <LPC17xx.h>
    #include "system_LPC17xx.c"
    
    #include <vector>
    
    
    class MENU{
        public:
        MENU(MENU *);               //constructor
    
        std::vector <MENU *> _items;
    };
    
    MENU::MENU(MENU *root)
    {
            _items.push_back(root); //IF I COMMENT THIS LINE THEN IT STARTS EXECUTION 
    };
    
    
    
    int main(void)
    {
            while (1) ;
    }
    

  • If you already have the heap initialized, then the above code should work.

    But are you sure that you know what you are doing? Should you use a vector for storing "root"? Doesn't every submenu only have one single root menu object? Wouldn't it be the root object that contains a list of submenu choices?

    On the other hand - having "next", "parent" and "childs" pointers in every menu object would allow you to link the menues without dynamic memory allocation for any vector.

  • You are right about everything that you said.
    This is actually not the code I'm using, it is just reduced to some part wich has basically no meaning, but it don't work and it should!

    Yes, I agree with you about not using vectors. I already decided to switch to full static initialization..

    Thanks for your help

  • Three things:
    1) When posting "examples" - make sure that the examples are valid for the discussion at had. Having simplified code that can't behave as you claim the code behaves means no one can help you based on the posted code.

    2) Are you sure that your heap is initialized before the global C++ objects gets initialized? If not, then your vector code will fail. Note that global C++ objects gets constructed before you reach main().

    3) Reduced or not - your code mixes up the owner of the lists and what elements to insert into the list.