• Tags
  • Documentation
  • Order
  • Register
  • Login
Duet3D Logo Duet3D
  • Tags
  • Documentation
  • Order
  • Register
  • Login

Issues defining a new command.

Scheduled Pinned Locked Moved
Firmware developers
4
5
240
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.
  • undefined
    jazbaatbadalgaye
    last edited by jazbaatbadalgaye 10 Jul 2021, 19:38 7 Oct 2021, 17:53

    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
    • undefined
      oliof
      last edited by 7 Oct 2021, 18:33

      I think you're not dereferencing your pointer correctly?

      <>RatRig V-Minion Fly Super5Pro RRF<> V-Core 3.1 IDEX k*****r <> RatRig V-Minion SKR 2 Marlin<>

      1 Reply Last reply Reply Quote 0
      • undefined
        dc42 administrators
        last edited by 7 Oct 2021, 19:05

        @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

        undefined undefined 2 Replies Last reply 7 Oct 2021, 19:39 Reply Quote 0
        • undefined
          jazbaatbadalgaye @dc42
          last edited by 7 Oct 2021, 19:39

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

          1 Reply Last reply Reply Quote 0
          • undefined
            zapta @dc42
            last edited by 8 Oct 2021, 05:41

            @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
            5 out of 5
            • First post
              5/5
              Last post
            Unless otherwise noted, all forum content is licensed under CC-BY-SA