Duet3D Logo

    Duet3D

    • Register
    • Login
    • Search
    • Categories
    • Tags
    • Documentation
    • Order

    Issues defining a new command.

    Firmware developers
    4
    5
    119
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • jazbaatbadalgaye
      jazbaatbadalgaye last edited by jazbaatbadalgaye

      I am trying to write a new M810 command to display an array. Here is what I did

      1. Declared a function which returns a pointer to an array.
      //In reprap.h 
      uint8_t *Gen() noexcept;
      uint8_t* my_pointer = Gen();
      
      1. Definition of the function
      //in reprap.cpp
      uint8_t *RepRap::Gen() noexcept
      {
      	uint8_t arr[3]={5};
      	return arr;
      }
      
      1. Declared the new Gcode function
      //in GCodes.h
      void GetData(const StringRef& reply) noexcept;
      
      1. 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]);
      	}	
      }
      
      1. 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
      
      1 Reply Last reply Reply Quote 0
      • oliof
        oliof last edited by

        I think you're not dereferencing your pointer correctly?

        <>Creality CR-20 IDEX Duet3 mini 5+<>RatRig V-Minion SKR RRF 1.1<>

        1 Reply Last reply Reply Quote 0
        • dc42
          dc42 administrators last edited by

          @jazbaatbadalgaye function Gen returns a pointer to data in local storage that ceases to exist when the function returns.

          Duet WiFi hardware designer and firmware engineer
          Please do not ask me for Duet support via PM or email, use the forum
          http://www.escher3d.com, https://miscsolutions.wordpress.com

          jazbaatbadalgaye zapta 2 Replies Last reply Reply Quote 0
          • jazbaatbadalgaye
            jazbaatbadalgaye @dc42 last edited by

            @dc42 Thanks! I fixed it by declaring the arr as static

            1 Reply Last reply Reply Quote 0
            • zapta
              zapta @dc42 last edited by

              @dc42 said in Issues defining a new command.:

              function Gen returns a pointer to data in local storage

              Some compiler flag this out

              https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-c4172?view=msvc-160

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Unless otherwise noted, all forum content is licensed under CC-BY-SA