Issues defining a new command.
-
I am trying to write a new M810 command to display an array. Here is what I did
- Declared a function which returns a pointer to an array.
//In reprap.h uint8_t *Gen() noexcept; uint8_t* my_pointer = Gen();
- Definition of the function
//in reprap.cpp uint8_t *RepRap::Gen() noexcept { uint8_t arr[3]={5}; return arr; }
- Declared the new Gcode function
//in GCodes.h void GetData(const StringRef& reply) noexcept;
- Definition of the new Gcode
//in GCodes.cpp void GCodes::GetData(const StringRef& reply) noexcept { for (size_t c=0; c<3;c++) { reply.catf("Arr: %u" PRIu8 "\n", reprap.my_pointer[c]); } }
- Added M810
//in GCodes2.cpp case 810: GetData(reply); break;
But when I send M810 on YAT, I get gibberish values like
Arr: 0hhu<LF>Arr: 0hhu<LF>Arr: 2hhu<LF>
What am I doing wrong?
Edit : Fixed it by declaring the
uint8_t arr[3]={5};
as
static uint8_t arr
-
I think you're not dereferencing your pointer correctly?
-
@jazbaatbadalgaye function Gen returns a pointer to data in local storage that ceases to exist when the function returns.
-
@dc42 Thanks! I fixed it by declaring the arr as static
-
@dc42 said in Issues defining a new command.:
function Gen returns a pointer to data in local storage
Some compiler flag this out