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

C++ simple question

I am using uVision 4.21 MDK_ARM to learn how to program C++ on MCB1700.

I have create a new project and add 2 files (startup_CPL17xx.s / system_LPCxx.c) to startup group. Then I add main.cpp to Sources Files group. The project options are:

Device: LPC 1768
Target: Default
Output: Default
Listing: Default
User: Default
C++: Default
Asm: Default
Linker: Default
Debug: Use ULINK Cortex Debugger
Utilities: Default

Then I change the startup_LPC17xx.s
Stack Size: 0x0000 1000
Heap Size: 0x0000 1000

My main.cpp code:

#include <vector>
#include <string>

int main (void)
{
  int i = 0;
  int sum = 0;
  std::vector<int> vInt;
  std::string str = "Hello, C++";

  vInt.push_back(1);
  vInt.push_back(2);
  vInt.push_back(3);
  vInt.push_back(4);
  vInt.push_back(5);

  sum = vInt.size();
  sum = 0;
  for(i=0; i<5; i++)
  {
    sum += vInt[i];
  }
  while(1);
  //return 0;
}


I build the project successfully and download it to MCB1700 board "OK". Then I hit "F9" to set break point at line "vInt.push_back(1);". After I select Debug->Start Debug Session, the break point is not reached even I hit "F5" many times. Why?

By the way, I find there are 2 windows: Disassembly Window and Code Window (for main.cpp). How could I step through the code on C++ source code level and how could I step through the code on Assembly level?

Parents
  • Thank you for your reply. I will look at it.

    By the way, for main.c case, why I can not watch the variables value? But I do find if I change the code to:

    int i = 0;
    int sum = 0;
    
    int main (void)
    {
    //      int i = 0;
    //      int sum = 0;
    
            for(i=0; i<10; i++)
            {
                    sum = sum + i;
            }
            i = sum;
            while(1);
            //return 0;
    }
    


    Move i / sum outside of the main, then I can watch the value change. Weird.

Reply
  • Thank you for your reply. I will look at it.

    By the way, for main.c case, why I can not watch the variables value? But I do find if I change the code to:

    int i = 0;
    int sum = 0;
    
    int main (void)
    {
    //      int i = 0;
    //      int sum = 0;
    
            for(i=0; i<10; i++)
            {
                    sum = sum + i;
            }
            i = sum;
            while(1);
            //return 0;
    }
    


    Move i / sum outside of the main, then I can watch the value change. Weird.

Children
  • You'll have to look at the assembly code the compiler produces. The debugger may not be able to tell how the variable is stored so it can't display it. It's probably just kept in a register for the duration of the loop.

    Making variables global forces the compiler to store them in actual memory locations and then you can inspect them.

    Usually switching optimisation off solves many of these types of problems but some optimisations are so basic the compiler will never avoid them.

    Frequently programmers will use 'volatile' to force variables into memory but in the case of a local variable on the stack that's rarely a good idea.

  • Why can't it inspect variables in registers, then?

    "some optimisations are so basic the compiler will never avoid them"

    So debug tools should really be able to cope with them!