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. pointer question

pointer question

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 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.
  • S Offline
    S Offline
    shaderx
    wrote on last edited by
    #1

    so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .

    C T Z S 4 Replies Last reply
    0
    • S shaderx

      so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .

      C Offline
      C Offline
      Chipperm
      wrote on last edited by
      #2

      If you want to use the pointer that way just declare the pointer as char* instead of void *. You could also just cast the pointer variable to char* if you really want to declare the pointer as void *.

      Chipper Martin

      S 1 Reply Last reply
      0
      • C Chipperm

        If you want to use the pointer that way just declare the pointer as char* instead of void *. You could also just cast the pointer variable to char* if you really want to declare the pointer as void *.

        Chipper Martin

        S Offline
        S Offline
        shaderx
        wrote on last edited by
        #3

        is there another way to shift it?

        1 Reply Last reply
        0
        • S shaderx

          so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          have you noticed that in your sample, p is not assigned, so memcpy() is writing nowhere (and there, will crash in a memory violation access) ? do p = new d; first...


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          S 1 Reply Last reply
          0
          • T toxcct

            have you noticed that in your sample, p is not assigned, so memcpy() is writing nowhere (and there, will crash in a memory violation access) ? do p = new d; first...


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            S Offline
            S Offline
            shaderx
            wrote on last edited by
            #5

            p is used in a buffer lock in DirectX Vertexbuffer->Lock((0,sizeof(g_cubeVertices),(void**)&p,0); so its good :P

            1 Reply Last reply
            0
            • S shaderx

              so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              Since you will probably be doing this in an array (I assume this is for DirectX vertices?) if st is an array of d's instead of single ones, just call it this way:

              d st[100];
              // fill in each st somewhere
              // p is allocated in a Vertex buffer
              memcpy(p, &st, sizeof(d) * 100);
              

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              S 1 Reply Last reply
              0
              • Z Zac Howland

                Since you will probably be doing this in an array (I assume this is for DirectX vertices?) if st is an array of d's instead of single ones, just call it this way:

                d st[100];
                // fill in each st somewhere
                // p is allocated in a Vertex buffer
                memcpy(p, &st, sizeof(d) * 100);
                

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                I'm not sure if this is what he's after; I think he wants to fill the array with multiple copies of the same element. On a side note, I'd do what you're trying to do like this: d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(st)); This way you can change the size of the array in one place and thus it's harder to make a mistake by only changing it in one.

                Steve

                Z 1 Reply Last reply
                0
                • S shaderx

                  so i got a pointer void *p; and a structure struct d{ blah....blah.. }; d st; and i use it with memcpy(p,&st,sizeof(d)); so my trouble is: after using memcpy i need to advance the pointer with sizeof(d), so i can copy another one. tryed p+=sizeof(d) but got : error C2036: 'void *' : unknown size help pls .

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  Don't use a void pointer, use a pointer of the correct type then use the ++ operator.

                  Steve

                  1 Reply Last reply
                  0
                  • S Stephen Hewitt

                    I'm not sure if this is what he's after; I think he wants to fill the array with multiple copies of the same element. On a side note, I'd do what you're trying to do like this: d st[100]; // fill in each st somewhere // p is allocated in a Vertex buffer memcpy(p, &st, sizeof(st)); This way you can change the size of the array in one place and thus it's harder to make a mistake by only changing it in one.

                    Steve

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    Stephen Hewitt wrote:

                    d st[100];// fill in each st somewhere// p is allocated in a Vertex buffermemcpy(p, &st, sizeof(st));

                    That works until you do something like this:

                    d* st = new d[100];
                    memcpy(p, st, sizeof(st)); // oops!  won't work
                    memcpy(p, st, sizeof(d) * 100); // works!
                    

                    If you want to change the size of the array, it is better to do this:

                    const unsigned long ARRAY_SIZE = 100;
                    d st[ARRAY_SIZE];
                    memcpy(p, st, sizeof(d) * ARRAY_SIZE);
                    

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    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