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. Strange problem with pointers

Strange problem with pointers

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 5 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.
  • A Offline
    A Offline
    anbluemoon
    wrote on last edited by
    #1

    I wrote something like this : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx; The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:

    vtx

    0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }

    origin::MD2Mesh::Vertex *

    after line "mdFrame->vertices = vtx" it becomes like this :

    vtx

    0x00434140 {v=0x00434140 "@AC" normIdx=0 }

    origin::MD2Mesh::Vertex *

    md2Frame->vertices

    0x00434140 {v=0x00434140 "@AC" normIdx=0 }

    origin::MD2Mesh::Vertex *

    Do u have any ideas ?

    P K A 3 Replies Last reply
    0
    • A anbluemoon

      I wrote something like this : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx; The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:

      vtx

      0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }

      origin::MD2Mesh::Vertex *

      after line "mdFrame->vertices = vtx" it becomes like this :

      vtx

      0x00434140 {v=0x00434140 "@AC" normIdx=0 }

      origin::MD2Mesh::Vertex *

      md2Frame->vertices

      0x00434140 {v=0x00434140 "@AC" normIdx=0 }

      origin::MD2Mesh::Vertex *

      Do u have any ideas ?

      P Offline
      P Offline
      PaulowniaK
      wrote on last edited by
      #2

      It looks like you've got some cyclic thing going on. You're setting vtx to something inside md2Frame. But vtx is cast from tmp which is obtained by manipulating buffer. But buffer is cast to md2Frame, in which you are trying to set this value... I can't say for certain that's the problem, but wouldn't hurt to sort this out properly first.

      Almost, but not quite, entirely unlike... me...

      1 Reply Last reply
      0
      • A anbluemoon

        I wrote something like this : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx; The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:

        vtx

        0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }

        origin::MD2Mesh::Vertex *

        after line "mdFrame->vertices = vtx" it becomes like this :

        vtx

        0x00434140 {v=0x00434140 "@AC" normIdx=0 }

        origin::MD2Mesh::Vertex *

        md2Frame->vertices

        0x00434140 {v=0x00434140 "@AC" normIdx=0 }

        origin::MD2Mesh::Vertex *

        Do u have any ideas ?

        K Offline
        K Offline
        KingsGambit
        wrote on last edited by
        #3

        What is type of the buffer? Is it uint8*? What expected by buffer + 40? See, buffer + 1 would point to next unint8 position in the buffer. Is that what intended?

        1 Reply Last reply
        0
        • A anbluemoon

          I wrote something like this : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer; uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp; md2Frame->vertices = vtx; The value of "vtx" is changed after the line "mdFrame->vertices = vtx" U can look at the following debugging table:

          vtx

          0x00434140 {v=0x00434140 "ÐBÄ" normIdx=0 }

          origin::MD2Mesh::Vertex *

          after line "mdFrame->vertices = vtx" it becomes like this :

          vtx

          0x00434140 {v=0x00434140 "@AC" normIdx=0 }

          origin::MD2Mesh::Vertex *

          md2Frame->vertices

          0x00434140 {v=0x00434140 "@AC" normIdx=0 }

          origin::MD2Mesh::Vertex *

          Do u have any ideas ?

          A Offline
          A Offline
          Avi Berger
          wrote on last edited by
          #4

          You haven't given us all the types and class/structure definitions involved. You are also using a mess of casts and potential aliasing issues. You have to know what you are doing and be very careful to get this kind of stuff right.

          anbluemoon wrote:

          MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer;

          Apparently, buffer is a SomeType_T *buffer set to the address of a block of allocated memory. What is SomeType_T? How big a block of memory? What is a MD2Mesh::Frame? How big is a MD2Mesh::Frame? Also if MD2Mesh::Frame is not a pod type, you are dead without properly constructing it via its constructor.

          anbluemoon wrote:

          uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp;

          Your magic number could be subject to breakage due to changes. Additionally, it hides your intent. Also don't forget about alignment issues and padding that the compiler can insert in structs. md2Frame now points to the beginning of the buffer. vtx now points inside the buffer.

          anbluemoon wrote:

          md2Frame->vertices = vtx;

          You have now changed part of the contents of the buffer. vtx has not changed at all. It is however a pointer, and what it points to in the buffer has changed because you wrote code to change it. Evidently, MD2Mesh::Frame.vertices is 40 * sizeof(SomeType_T) bytes from the beginning of the structure.

          Please do not read this signature.

          A 1 Reply Last reply
          0
          • A Avi Berger

            You haven't given us all the types and class/structure definitions involved. You are also using a mess of casts and potential aliasing issues. You have to know what you are doing and be very careful to get this kind of stuff right.

            anbluemoon wrote:

            MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer;

            Apparently, buffer is a SomeType_T *buffer set to the address of a block of allocated memory. What is SomeType_T? How big a block of memory? What is a MD2Mesh::Frame? How big is a MD2Mesh::Frame? Also if MD2Mesh::Frame is not a pod type, you are dead without properly constructing it via its constructor.

            anbluemoon wrote:

            uint8* tmp = buffer + 40; MD2Mesh::Vertex* vtx = (MD2Mesh::Vertex*)tmp;

            Your magic number could be subject to breakage due to changes. Additionally, it hides your intent. Also don't forget about alignment issues and padding that the compiler can insert in structs. md2Frame now points to the beginning of the buffer. vtx now points inside the buffer.

            anbluemoon wrote:

            md2Frame->vertices = vtx;

            You have now changed part of the contents of the buffer. vtx has not changed at all. It is however a pointer, and what it points to in the buffer has changed because you wrote code to change it. Evidently, MD2Mesh::Frame.vertices is 40 * sizeof(SomeType_T) bytes from the beginning of the structure.

            Please do not read this signature.

            A Offline
            A Offline
            anbluemoon
            wrote on last edited by
            #5

            Here is data structures: typedef unsigned char uint8; class MD2Mesh { struct Vertex { uint8 coord[3], normalIdx; }; struct Frame { float translation[3], rotation[3]; char name[16]; Mesh::Vertex* vertices; }; }; At first I tried to do something like : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer to get the values from the buffer without initializing the pointer and I managed to get the values of translation, rotation and name but the vertices address didn't point to the "buffer + 40(size of translation + rotation + name)" so i just pass the the address of vtx which is buffer + 40

            M 1 Reply Last reply
            0
            • A anbluemoon

              Here is data structures: typedef unsigned char uint8; class MD2Mesh { struct Vertex { uint8 coord[3], normalIdx; }; struct Frame { float translation[3], rotation[3]; char name[16]; Mesh::Vertex* vertices; }; }; At first I tried to do something like : MD2Mesh::Frame* md2Frame = (MD2Mesh::Frame*)buffer to get the values from the buffer without initializing the pointer and I managed to get the values of translation, rotation and name but the vertices address didn't point to the "buffer + 40(size of translation + rotation + name)" so i just pass the the address of vtx which is buffer + 40

              M Offline
              M Offline
              Mohan Ramachandra
              wrote on last edited by
              #6

              Again you forgot to mention the type of buffer?

              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