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. Question about increment a Void* pointer

Question about increment a Void* pointer

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
7 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.
  • N Offline
    N Offline
    Nacho Chip
    wrote on last edited by
    #1

    Hello all, I have a question about doing an increment of a Void* pointer. I find out that I cannot do something like this int i[10]; void* p = i; int j = *((int*)(p++)); The compiler will complain about the size of the pointer when I try to do an increment. My alternative approach is int i[10]; void* p = i; int j = *((int*)(((char*)(p))++)); // Ugly First, I type cast the void* to char*, and then the compiler will know when I do the increment, it is going to add 4 bytes each time. I think this approach is ugly and might not compatiable with different platforms (when char* is not 4 bytes). In case if people wonder why I try to do something like this is that I try to calculate the offset in an array by my self with only know the size of the element in the array in the array. However, I don't know how to add exactly n byte to a pointer when calculate the offset. The default increment behavior always requires to know the type of the pointer, which I don't know. Any idea? Thanks! Nacho

    J B S R 4 Replies Last reply
    0
    • N Nacho Chip

      Hello all, I have a question about doing an increment of a Void* pointer. I find out that I cannot do something like this int i[10]; void* p = i; int j = *((int*)(p++)); The compiler will complain about the size of the pointer when I try to do an increment. My alternative approach is int i[10]; void* p = i; int j = *((int*)(((char*)(p))++)); // Ugly First, I type cast the void* to char*, and then the compiler will know when I do the increment, it is going to add 4 bytes each time. I think this approach is ugly and might not compatiable with different platforms (when char* is not 4 bytes). In case if people wonder why I try to do something like this is that I try to calculate the offset in an array by my self with only know the size of the element in the array in the array. However, I don't know how to add exactly n byte to a pointer when calculate the offset. The default increment behavior always requires to know the type of the pointer, which I don't know. Any idea? Thanks! Nacho

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      Rather than using a void*, I suggest using a BYTE*. you can then add whatever size you need without worrying about casting. You then simply increment the pointer by doing operations comparable to: p += sizeof(int); Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

      N 1 Reply Last reply
      0
      • J Joe Woodbury

        Rather than using a void*, I suggest using a BYTE*. you can then add whatever size you need without worrying about casting. You then simply increment the pointer by doing operations comparable to: p += sizeof(int); Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

        N Offline
        N Offline
        Nacho Chip
        wrote on last edited by
        #3

        Thanks, but is there any way which just use ANSI C++ standard type rather than BYTE? By using BYTE, we will have problem when we port the code in other platforms. Any idea? Nacho

        J 1 Reply Last reply
        0
        • N Nacho Chip

          Thanks, but is there any way which just use ANSI C++ standard type rather than BYTE? By using BYTE, we will have problem when we port the code in other platforms. Any idea? Nacho

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          Use unsigned char. (Or use BYTE and on platforms that do not have it defined or typedefed, typedef it yourself.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          1 Reply Last reply
          0
          • N Nacho Chip

            Hello all, I have a question about doing an increment of a Void* pointer. I find out that I cannot do something like this int i[10]; void* p = i; int j = *((int*)(p++)); The compiler will complain about the size of the pointer when I try to do an increment. My alternative approach is int i[10]; void* p = i; int j = *((int*)(((char*)(p))++)); // Ugly First, I type cast the void* to char*, and then the compiler will know when I do the increment, it is going to add 4 bytes each time. I think this approach is ugly and might not compatiable with different platforms (when char* is not 4 bytes). In case if people wonder why I try to do something like this is that I try to calculate the offset in an array by my self with only know the size of the element in the array in the array. However, I don't know how to add exactly n byte to a pointer when calculate the offset. The default increment behavior always requires to know the type of the pointer, which I don't know. Any idea? Thanks! Nacho

            B Offline
            B Offline
            bujji_bec
            wrote on last edited by
            #5

            arey yar that above one Immpossible. v.raju

            1 Reply Last reply
            0
            • N Nacho Chip

              Hello all, I have a question about doing an increment of a Void* pointer. I find out that I cannot do something like this int i[10]; void* p = i; int j = *((int*)(p++)); The compiler will complain about the size of the pointer when I try to do an increment. My alternative approach is int i[10]; void* p = i; int j = *((int*)(((char*)(p))++)); // Ugly First, I type cast the void* to char*, and then the compiler will know when I do the increment, it is going to add 4 bytes each time. I think this approach is ugly and might not compatiable with different platforms (when char* is not 4 bytes). In case if people wonder why I try to do something like this is that I try to calculate the offset in an array by my self with only know the size of the element in the array in the array. However, I don't know how to add exactly n byte to a pointer when calculate the offset. The default increment behavior always requires to know the type of the pointer, which I don't know. Any idea? Thanks! Nacho

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

              Why are you using a void pointer? If you want to point to an int why not use an int*? Since void* can point to anything the compiler has no way of knowing the size of the object pointed to. In your code when you done p++ how far did you expect the pointer to be advanced? Steve

              1 Reply Last reply
              0
              • N Nacho Chip

                Hello all, I have a question about doing an increment of a Void* pointer. I find out that I cannot do something like this int i[10]; void* p = i; int j = *((int*)(p++)); The compiler will complain about the size of the pointer when I try to do an increment. My alternative approach is int i[10]; void* p = i; int j = *((int*)(((char*)(p))++)); // Ugly First, I type cast the void* to char*, and then the compiler will know when I do the increment, it is going to add 4 bytes each time. I think this approach is ugly and might not compatiable with different platforms (when char* is not 4 bytes). In case if people wonder why I try to do something like this is that I try to calculate the offset in an array by my self with only know the size of the element in the array in the array. However, I don't know how to add exactly n byte to a pointer when calculate the offset. The default increment behavior always requires to know the type of the pointer, which I don't know. Any idea? Thanks! Nacho

                R Offline
                R Offline
                Rilhas
                wrote on last edited by
                #7

                The arithmetic for void* is not defined, so if you need pointer arithmetic you must do it with a type other than void*. However, the void* is guaranteed to be capable of holding any address on the machine, while other types of pointers do not guarantee it. But, for practical purposes on practical platforms with practical processors you may consider that all pointers are interchangeable. This means that your code should be re-written to: int i[10]; void* p = i; int j = * (((int*)p)+1); In other words: first cast the void* to int*. On practical systems this conversion does not produce any machine instructions, so it is not really executable code. This means that performance is not an issue. After the cast the compiler treats the pointer as an int*, so the "+1" arithmetic is well defined (equivalent to "++"). It will be, in fact, equivalent to advancing the pointer to the address where the next int lies. In Win32 this will be an increment of 4 bytes. I've seen SOLARIS implementations where this same code would produce an increment of 8 bytes, to correctly handle its 8 byte int's. The code I wrote is perfectly legal ANSI, compiles and runs correctly on all platforms, and I wouldn't consider it ugly. Your original code too, except that the precedence is not correct (you increment while the pointer is still void* and at that point it has no arithmetic logic). Anyway, the second piece of code you propose should not compile. But that can be easilly fixed with "+1" instead of "++". But your reasoning changed in the second example, since you forced the cast from void* (that has no arithmetic logic) to char* (that has arithmetic logic) before incrementing, and so the increment is well defined. However, the increment amount is not correct, because sizeof(char) is 1 and not 4. The char*++ points to the next character (1 byte), instead of the next int (4 bytes). That should have resulted in a bad value for j. Didn't it? Anyway, below is a piece of code you can test. I initialized "i" to show you that "j" gets a bad value (after fixing from "++" to "+1"). Also, you can see that "k" and "m" get the correct value. As a final note, the code for all 3 integers "j", "k", and "m" as the same instruction type and count, and their efficiency is the same. Of course "j" is wrong, but the compiler doesn't know that (however, some processors may generate exceptions when integers are not aligned, which may result in crashes or slow operating system exception handling). The "ip" and "n" are not as efficient as the cas

                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