Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to pass array of byte?

How to pass array of byte?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    Joe Smith IX
    wrote on last edited by
    #1

    I am trying to pass an array of binary data from one function to another, but it fails. Why?

    void C1:Caller()
    {
    BYTE *pTemplate = NULL; DWORD dwSize=0;
    GetData(pTemplate, dwSize);
    // in here dwSize=1024, which is correct
    // but pTemplate is NULL! Why?
    }

    void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
    {
    delete pTemplate;
    pTemplate = NULL;

    dwSize = 1024;
    pTemplate = new BYTE[dwSize];
    // ... fill pTemplate with desired data
    }

    Anyone can help me here? Thanks.

    C D 2 Replies Last reply
    0
    • J Joe Smith IX

      I am trying to pass an array of binary data from one function to another, but it fails. Why?

      void C1:Caller()
      {
      BYTE *pTemplate = NULL; DWORD dwSize=0;
      GetData(pTemplate, dwSize);
      // in here dwSize=1024, which is correct
      // but pTemplate is NULL! Why?
      }

      void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
      {
      delete pTemplate;
      pTemplate = NULL;

      dwSize = 1024;
      pTemplate = new BYTE[dwSize];
      // ... fill pTemplate with desired data
      }

      Anyone can help me here? Thanks.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      If you want to only pass the content of the array, then your method is fine. But as you want to allocate the a new array inside the function, you need to pass the array by reference. Think of it this way: a pointer is more or less the same as an integer containing an address. If you pass the integer value by value, a copy will be made in the function and the function will modify the copy and leave the original intact. It is the same with pointers: as long as you only want to access the content of the pointed memry, no problem. But if you want to assign a new address (like new is doing), then you have to pass it by reference:

      void C1:GetData(BYTE* &pTemplate, DWORD &dwSize)

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      J 1 Reply Last reply
      0
      • C Cedric Moonen

        If you want to only pass the content of the array, then your method is fine. But as you want to allocate the a new array inside the function, you need to pass the array by reference. Think of it this way: a pointer is more or less the same as an integer containing an address. If you pass the integer value by value, a copy will be made in the function and the function will modify the copy and leave the original intact. It is the same with pointers: as long as you only want to access the content of the pointed memry, no problem. But if you want to assign a new address (like new is doing), then you have to pass it by reference:

        void C1:GetData(BYTE* &pTemplate, DWORD &dwSize)

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        J Offline
        J Offline
        Joe Smith IX
        wrote on last edited by
        #3

        I knew I was missing something simple, but I do not expect it to be that simple. Thanks a lot.

        1 Reply Last reply
        0
        • J Joe Smith IX

          I am trying to pass an array of binary data from one function to another, but it fails. Why?

          void C1:Caller()
          {
          BYTE *pTemplate = NULL; DWORD dwSize=0;
          GetData(pTemplate, dwSize);
          // in here dwSize=1024, which is correct
          // but pTemplate is NULL! Why?
          }

          void C1:GetData(BYTE *pTemplate, DWORD &dwSize)
          {
          delete pTemplate;
          pTemplate = NULL;

          dwSize = 1024;
          pTemplate = new BYTE[dwSize];
          // ... fill pTemplate with desired data
          }

          Anyone can help me here? Thanks.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Joe Smith IX wrote:

          Anyone can help me here?

          Another solution is to pass the address of pTemplate to GetData().

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups