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. pointers and array of pointers question

pointers and array of pointers question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformancehelp
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.
  • K Offline
    K Offline
    kitty5
    wrote on last edited by
    #1

    originally i had: UCHAR *buff1 = 0, *buff2 = 0,*buff3 = 0; buff1 = new UCHAR[45000000]; and I used it: ReadFile(...,...,buff1+offset,...,NULL); and that was fine. then I changed it so that it's more flexible: UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL); buff[num]+= 2028;//increment the pointer to the next chunk I get the error code "998" which is "Invalid access to memory location." could someone please explain to me what I'm doing wrong? thanks!

    Kitty5

    D C C 4 Replies Last reply
    0
    • K kitty5

      originally i had: UCHAR *buff1 = 0, *buff2 = 0,*buff3 = 0; buff1 = new UCHAR[45000000]; and I used it: ReadFile(...,...,buff1+offset,...,NULL); and that was fine. then I changed it so that it's more flexible: UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL); buff[num]+= 2028;//increment the pointer to the next chunk I get the error code "998" which is "Invalid access to memory location." could someone please explain to me what I'm doing wrong? thanks!

      Kitty5

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

      kitty5 wrote:

      ReadFile(...,...,buff1+offset,...,NULL);

      You are reading into an invalid memory location. You must allocate memory for buff1, or assign it to a valid memory location, before it can be used in this fashion.


      "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

      "Judge not by the eye but by the heart." - Native American Proverb

      K 1 Reply Last reply
      0
      • D David Crow

        kitty5 wrote:

        ReadFile(...,...,buff1+offset,...,NULL);

        You are reading into an invalid memory location. You must allocate memory for buff1, or assign it to a valid memory location, before it can be used in this fashion.


        "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

        "Judge not by the eye but by the heart." - Native American Proverb

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

        DavidCrow wrote:

        You are reading into an invalid memory location. You must allocate memory for buff1, or assign it to a valid memory location, before it can be used in this fashion.

        I do have: buff1 = new UCHAR[45000000];. but that works.... it's when I try to use an array of pointers that i run into trouble with.... instead of buff1 i do, buff[0]....

        Kitty5

        1 Reply Last reply
        0
        • K kitty5

          originally i had: UCHAR *buff1 = 0, *buff2 = 0,*buff3 = 0; buff1 = new UCHAR[45000000]; and I used it: ReadFile(...,...,buff1+offset,...,NULL); and that was fine. then I changed it so that it's more flexible: UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL); buff[num]+= 2028;//increment the pointer to the next chunk I get the error code "998" which is "Invalid access to memory location." could someone please explain to me what I'm doing wrong? thanks!

          Kitty5

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

          kitty5 wrote:

          UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL);

          buff is an array of 'size' 3 and each cell in the array contain a pointer (and each of these pointers point to an array of 45000000 UCHAR). When you do do buff[num], if num is higher than the size of your array (3 in this case), you write to an invalid address. By the way, allocating 3 times 45000000 is quite big. Are you sure you want to allocate so much memory :omg: ?


          Cédric Moonen Software developer
          Charting control [Updated - v1.1]

          1 Reply Last reply
          0
          • K kitty5

            originally i had: UCHAR *buff1 = 0, *buff2 = 0,*buff3 = 0; buff1 = new UCHAR[45000000]; and I used it: ReadFile(...,...,buff1+offset,...,NULL); and that was fine. then I changed it so that it's more flexible: UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL); buff[num]+= 2028;//increment the pointer to the next chunk I get the error code "998" which is "Invalid access to memory location." could someone please explain to me what I'm doing wrong? thanks!

            Kitty5

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            kitty5 wrote:

            buff[num]+= 2028;

            that's a bad idea. since you haven't stored the pointer in buff[num] anywhere else, you won't be able to free buff[num] later. you should make a copy of buff[num] so you'll be able to delete it when you're done.

            kitty5 wrote:

            //increment the pointer to the next chunk

            how many 'chunks' do you get before the error happens ?

            image processing | blogging

            1 Reply Last reply
            0
            • K kitty5

              originally i had: UCHAR *buff1 = 0, *buff2 = 0,*buff3 = 0; buff1 = new UCHAR[45000000]; and I used it: ReadFile(...,...,buff1+offset,...,NULL); and that was fine. then I changed it so that it's more flexible: UCHAR *buff[3]; buff[0] = new UCHAR[45000000]; buff[1] = new UCHAR[45000000]; buff[2] = new UCHAR[45000000]; when I do: ReadFile(...,...,buff[num],...,NULL); buff[num]+= 2028;//increment the pointer to the next chunk I get the error code "998" which is "Invalid access to memory location." could someone please explain to me what I'm doing wrong? thanks!

              Kitty5

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

              kitty5 wrote:

              buff[num]+= 2028;//increment the pointer to the next chunk

              You can't do this and expect to be able to delete the memory. The statement buff[0] = new UCHAR[45000000] assigned a specific memory address to buff[0]. If you advance that address by 2048 bytes, the delete operator will fail. Try:

              ReadFile(..., buff[num]+offset, ...);
              offset += 2048;


              "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

              "Judge not by the eye but by the heart." - Native American Proverb

              K 1 Reply Last reply
              0
              • D David Crow

                kitty5 wrote:

                buff[num]+= 2028;//increment the pointer to the next chunk

                You can't do this and expect to be able to delete the memory. The statement buff[0] = new UCHAR[45000000] assigned a specific memory address to buff[0]. If you advance that address by 2048 bytes, the delete operator will fail. Try:

                ReadFile(..., buff[num]+offset, ...);
                offset += 2048;


                "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                "Judge not by the eye but by the heart." - Native American Proverb

                K Offline
                K Offline
                kitty5
                wrote on last edited by
                #7

                DavidCrow wrote:

                You can't do this and expect to be able to delete the memory. The statement buff[0] = new UCHAR[45000000] assigned a specific memory address to buff[0]. If you advance that address by 2048 bytes, the delete operator will fail. Try: ReadFile(..., buff[num]+offset, ...);offset += 2048;

                isn't: offset = 2028; ReadFile(..., buff[num]+offset, ...); not the same as: offset = 2028; buff[num]+= offset; ReadFile(..., buff[num], ...); either way you are advancing the address by 2028?

                Kitty5

                D J 2 Replies Last reply
                0
                • K kitty5

                  DavidCrow wrote:

                  You can't do this and expect to be able to delete the memory. The statement buff[0] = new UCHAR[45000000] assigned a specific memory address to buff[0]. If you advance that address by 2048 bytes, the delete operator will fail. Try: ReadFile(..., buff[num]+offset, ...);offset += 2048;

                  isn't: offset = 2028; ReadFile(..., buff[num]+offset, ...); not the same as: offset = 2028; buff[num]+= offset; ReadFile(..., buff[num], ...); either way you are advancing the address by 2028?

                  Kitty5

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

                  One actually advances the pointer, while the other does not. That's the distinction.


                  "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • K kitty5

                    DavidCrow wrote:

                    You can't do this and expect to be able to delete the memory. The statement buff[0] = new UCHAR[45000000] assigned a specific memory address to buff[0]. If you advance that address by 2048 bytes, the delete operator will fail. Try: ReadFile(..., buff[num]+offset, ...);offset += 2048;

                    isn't: offset = 2028; ReadFile(..., buff[num]+offset, ...); not the same as: offset = 2028; buff[num]+= offset; ReadFile(..., buff[num], ...); either way you are advancing the address by 2028?

                    Kitty5

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9

                    When you allocate a chunk of memory, what you get is a memory chunk at least as big as what you requested, and some extra data. The whole chunk looks like this:

                    [ Extra Data ][ Your data - X bytes ]

                    The pointer which new returns, is the adress of the first byte of [ Your data ]. The [ Extra Data ] chunk is used later on when you deallocate the memory. When you pass a pointer to delete for deallocation, delete will actually "step back" and take a peek at what's inside [ Extra Data ]. That chunk of data contains book keeping information about the memory allocation - which it really needs to be able to put the allocated memory back into the pool of unallocated memory. If you increment the pointer value, it will no longer point to the beginning of the data chunk which new returned. So when you pass the modified pointer to delete, it will not find the book keeping information when it "steps back". It will find data from which you put into your data chunk, which delete will interpret as garbage...

                    -- Torn from tomorrow's headlines

                    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