Simple eval board project std::map not working

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple eval board project std::map not working

238 Views
antonypace
Contributor II

Hi, I'm not new to embedded or NXP development but am very new to Eclipse and MCUXpresso. I have a MIMXRT1020-EVK development board.

I'm trying to get some simple C++ code running on it and have a problem with std::map.  

Here is a minimal version of my code:

#include <iostream>
#include <list>
#include <map>
int main()
{
    // list test
    std::list<int> myList;
    myList.push_back(33);
    std::cout << "list:" << myList.front() << std::endl;
    // map test
    std::map<int,int> myMap;
    myMap[3] = 3;
    std::cout << "map:" << myMap[3] << std::endl;
}

The output I get is

list:33

map:0

Obviously the map should be giving me 3 instead of 0.

I have tried many things but it's all so new to me I don't know my way around the IDE yet.

I've tried

  • Different language standards, currently ISO C++14
  • Different libraries, currently Newlib (Auto)
  • Using map::at() which throws an exception
  • Inserting 3,3 as a std::pair
  • Initialising the map with {{3,3}}
  • Iterating over the map
  • Fairly random trying settings 

I've tried std::set as I know that is based on a RedBlack tree too and that also failed. With two entries it iterated only over one of them. If I read the size() it says 2.

Can anyone advise me what I'm doing wrong or what area I should look in? I could understand if none of the STL worked but for list to work and map not really baffles me.

Many thanks

Ant

0 Kudos
3 Replies

99 Views
antonypace
Contributor II

@Con Verse Many thanks for that suggestion. I expect we are going to make good use of streams in our embedded code for things other than print and we currently have plenty of space but I'll bear that in mind and do some experiments.

SOLUTION and more questions.

I found that all my strangeness went away when I switched from NewLib standard library to NewLibNano. Does anyone know why that should be? Does that mean I can't use NewLib in my code? Does that mean there are missing features?

0 Kudos

99 Views
FelipeGarcia
NXP TechSupport
NXP TechSupport

Hi Anthony,

 

I recommend you to check the MCUXpresso IDE User Guide in chapter 17. Here it is explained the differences among the C/C++ Libraries supported by MCUXpresso (Redlib, Newlib and NewlibNano).

 

https://www.nxp.com/docs/en/user-guide/MCUXpresso_IDE_User_Guide.pdf

 

Hope it helps!

 

Best regards,

Felipe

99 Views
converse
Senior Contributor IV

I don't know if this will help, but try using printf() to output the value. C++ I/O is *HUGE* (in the context of a small-memory device like and MCU), so you a to going to want to use it anyway...

As a hint - take a look at your executable size before and after using printf.

0 Kudos