Sunday, 15 January 2012

C/C++ string memory allocation -



C/C++ string memory allocation -

i've begun c/c++ development on embedded scheme (arm - stm32f4 more specific) , having classical problems of isn't used c or low level memory managment.

basically have class menuoption inherits field other class , looks this:

... char text[20]; ... void menuoption::settext(const char* text1) { clearcurrent(); needsupdate = true; strncpy(text, text1, 20); width = font->fontwidth*strlen(text); }

the constructor class calls settext method store text. works fine if utilize within main function:

std::vector<menuoption *> mainmenuoptions; menuoption* op1 = new menuoption(13, 15, "info", white, black); op1->setselected(true); mainmenuoptions.push_back(op1);

but fails when want utilize this:

std::vector<menuoption *> options; (int = 0; < things.size(); i++) { thing *th = things[i]; ... stuff th ... menuoption* op = new menuoption(190, 38+25*i, "test", white, black); options.push_back(op); }

this fails (debugger sort of stalls) @ menuoption* op ... line. guessing isn't should doing. can't seem find working solution.

edit:

to reply everyones questions. in fact compile c++ compiler. gcc using c++11 dialect.

there reason why using c strings instead of std::string. using few c libraries need c strings. , whenever tried convert string c string within freertos task thing fail. same problem actually.

no other breakpoint within task trigger after reaches constructor line. can't step in or skip line or @ line. have feeling gets caught hard_fault interrupt handler. other tasks go on run. that's problem. there no errors or point me cause. same problem when using std::string when tried create new menuoption within freertos task. thing works if remove string constructor. guessing has strings.

as string length. know strings used here not longer 15 characters. used 5 characters pure "backup"-

as ... stuff th ... this: th->flag = true;. didn't more because of problem.

the tip posted in comments Étienne lead me answer. more found this: http://www.freertos.org/freertos_support_forum_archive/october_2013/freertos_using_c_std_vector_in_task_93928e86j.html

void *operator new(size_t size) { void *p; if(uxtaskgetnumberoftasks()) p=pvportmalloc(size); else p=malloc(size); homecoming p; } void operator delete(void *p) { if(uxtaskgetnumberoftasks()) vportfree( p ); else free( p ); p = null; }

c++ string stm32

No comments:

Post a Comment