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. dynamically allocted array question

dynamically allocted array question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
11 Posts 4 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 kitty5

    if I do: #DEFINE BUFF_SIZE 0x400; //# of bytes to read ULONG *buff = null, numBytesRead = 0; buff = new ULONG[11250000]; //total of 11,250,000 32-bit words Handle hdl = INVALID_HANDLE_VALUE; ... while(...){ ReadFile(hndlPciAlt, buff, BUFF_SIZE, numBytesRead, NULL); //DMA data xfer ... } **Where:** BOOl ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped ); is a windows function. Questions: 1) So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it? 2) Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile(). 3) If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

    Kitty5

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

    kitty5 wrote:

    1. So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it?

    No, it copy the data at the address you pass to the function, which is the begining of your array. If you want to append it, you'll have to increment the address (something like buff+Offset). But warning !! As your buffer is a ULONG, it means that a single increment will point to the next ULONG (so 4 bytes further). It is not a good idea in this case to use a pointer to ULONG, use a pointer to UCHAR instead so you don't have to cast the pointer to a char area in order to be able to increment only one byte. Remember that ReadFile read data un bytes, so it can read only 3 bytes for example (and there you got the problem because it is not a multiple of 4)

    kitty5 wrote:

    1. Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile().

    ReadFile is supposed to read bytes, so it has nothing to do with 32-bit words.

    kitty5 wrote:

    1. If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

    No, it is the number of bytes to read. The function won't read more bytes (yup, I checked MSDN and that's strange they say minimum instead of maximum :~ )


    Cédric Moonen Software developer
    Charting control

    S K 2 Replies Last reply
    0
    • K kitty5

      if I do: #DEFINE BUFF_SIZE 0x400; //# of bytes to read ULONG *buff = null, numBytesRead = 0; buff = new ULONG[11250000]; //total of 11,250,000 32-bit words Handle hdl = INVALID_HANDLE_VALUE; ... while(...){ ReadFile(hndlPciAlt, buff, BUFF_SIZE, numBytesRead, NULL); //DMA data xfer ... } **Where:** BOOl ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped ); is a windows function. Questions: 1) So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it? 2) Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile(). 3) If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

      Kitty5

      S Offline
      S Offline
      Steen Krogsgaard
      wrote on last edited by
      #3
      1. it will overwrite. To append, use lpNumberOfBytesRead to offset the buffer pointer in subsequent calls to ReadFile. 2) Yes. lpBuffer is a void pointer, so it will be handled as an array of bytes. If the endian-ness of the words you read is correct you will get an array of 256 ULONGS when you read 1024 bytes. 3) It will read exactly nNumberOfBytesToRead. ReadFile will wait until it has read these bytes (assuming you don't use overlapped I/O, but that's another story), but it will not read more than this number of bytes (this would also wrech havoc on your buffer)

      Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

      C 1 Reply Last reply
      0
      • C Cedric Moonen

        kitty5 wrote:

        1. So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it?

        No, it copy the data at the address you pass to the function, which is the begining of your array. If you want to append it, you'll have to increment the address (something like buff+Offset). But warning !! As your buffer is a ULONG, it means that a single increment will point to the next ULONG (so 4 bytes further). It is not a good idea in this case to use a pointer to ULONG, use a pointer to UCHAR instead so you don't have to cast the pointer to a char area in order to be able to increment only one byte. Remember that ReadFile read data un bytes, so it can read only 3 bytes for example (and there you got the problem because it is not a multiple of 4)

        kitty5 wrote:

        1. Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile().

        ReadFile is supposed to read bytes, so it has nothing to do with 32-bit words.

        kitty5 wrote:

        1. If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

        No, it is the number of bytes to read. The function won't read more bytes (yup, I checked MSDN and that's strange they say minimum instead of maximum :~ )


        Cédric Moonen Software developer
        Charting control

        S Offline
        S Offline
        Steen Krogsgaard
        wrote on last edited by
        #4

        damn. You beat me to it :)

        Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

        1 Reply Last reply
        0
        • S Steen Krogsgaard
          1. it will overwrite. To append, use lpNumberOfBytesRead to offset the buffer pointer in subsequent calls to ReadFile. 2) Yes. lpBuffer is a void pointer, so it will be handled as an array of bytes. If the endian-ness of the words you read is correct you will get an array of 256 ULONGS when you read 1024 bytes. 3) It will read exactly nNumberOfBytesToRead. ReadFile will wait until it has read these bytes (assuming you don't use overlapped I/O, but that's another story), but it will not read more than this number of bytes (this would also wrech havoc on your buffer)

          Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

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

          Steen Krogsgaard wrote:

          ReadFile will wait until it has read these bytes (assuming you don't use overlapped I/O, but that's another story),

          Not necessarily. For example, using a serial port you can configure it with some specific timeouts and it often happens that the read function will time out before having read all the data. But of course, you can configure it with an infinite timeout also (and in that case, it will follow what you described).


          Cédric Moonen Software developer
          Charting control

          S 1 Reply Last reply
          0
          • C Cedric Moonen

            Steen Krogsgaard wrote:

            ReadFile will wait until it has read these bytes (assuming you don't use overlapped I/O, but that's another story),

            Not necessarily. For example, using a serial port you can configure it with some specific timeouts and it often happens that the read function will time out before having read all the data. But of course, you can configure it with an infinite timeout also (and in that case, it will follow what you described).


            Cédric Moonen Software developer
            Charting control

            S Offline
            S Offline
            Steen Krogsgaard
            wrote on last edited by
            #6

            Right. In any case, it won't read more than the number of bytes specified. Will ReadFile return true if there is a timeout?

            Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

            C 1 Reply Last reply
            0
            • S Steen Krogsgaard

              Right. In any case, it won't read more than the number of bytes specified. Will ReadFile return true if there is a timeout?

              Cheers Steen. "Are you gonna check your makeup when you're done whining?" John Simmons, 05/31/2006

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

              Steen Krogsgaard wrote:

              Will ReadFile return true if there is a timeout?

              Yes, it returns true only if an error occurs. A timeout in that case is not an error.


              Cédric Moonen Software developer
              Charting control

              1 Reply Last reply
              0
              • C Cedric Moonen

                kitty5 wrote:

                1. So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it?

                No, it copy the data at the address you pass to the function, which is the begining of your array. If you want to append it, you'll have to increment the address (something like buff+Offset). But warning !! As your buffer is a ULONG, it means that a single increment will point to the next ULONG (so 4 bytes further). It is not a good idea in this case to use a pointer to ULONG, use a pointer to UCHAR instead so you don't have to cast the pointer to a char area in order to be able to increment only one byte. Remember that ReadFile read data un bytes, so it can read only 3 bytes for example (and there you got the problem because it is not a multiple of 4)

                kitty5 wrote:

                1. Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile().

                ReadFile is supposed to read bytes, so it has nothing to do with 32-bit words.

                kitty5 wrote:

                1. If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

                No, it is the number of bytes to read. The function won't read more bytes (yup, I checked MSDN and that's strange they say minimum instead of maximum :~ )


                Cédric Moonen Software developer
                Charting control

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

                Cedric Moonen wrote:

                If you want to append it, you'll have to increment the address (something like buff+Offset). But warning !! As your buffer is a ULONG, it means that a single increment will point to the next ULONG (so 4 bytes further). It is not a good idea in this case to use a pointer to ULONG, use a pointer to UCHAR instead so you don't have to cast the pointer to a char area in order to be able to increment only one byte.

                Ok so I do: #define BUFF_SIZE = 0X400; ULONG numBytesRead = 0, offset = 0; UCHAR *buff = null; buff = new UCHAR[45000000]; while(...) { ReadFile(hndlPciAlt, buff, BUFF_SIZE, numBytesRead, NULL); //DMA data xfer } how would I pass the offsetted buff address in the while loop? (i.e. buff + numBytesRead) Since, every time I got into the loop it will be buff then buff + numBytesRead then buff + numBytesRead + numBytesRead, ... etc. can I: #define BUFF_SIZE = 0X400; ULONG numBytesRead = 0, offset = 0; UCHAR *buff = null; buff = new UCHAR[45000000]; while(...) { ReadFile(hndlPciAlt, (buff + offset), BUFF_SIZE, numBytesRead, NULL); //DMA data xfer offset += numBytesRead; } Is (buff + offset) the proper syntax to increment the address of buff? Thanks. :-D

                Kitty5

                C 1 Reply Last reply
                0
                • K kitty5

                  Cedric Moonen wrote:

                  If you want to append it, you'll have to increment the address (something like buff+Offset). But warning !! As your buffer is a ULONG, it means that a single increment will point to the next ULONG (so 4 bytes further). It is not a good idea in this case to use a pointer to ULONG, use a pointer to UCHAR instead so you don't have to cast the pointer to a char area in order to be able to increment only one byte.

                  Ok so I do: #define BUFF_SIZE = 0X400; ULONG numBytesRead = 0, offset = 0; UCHAR *buff = null; buff = new UCHAR[45000000]; while(...) { ReadFile(hndlPciAlt, buff, BUFF_SIZE, numBytesRead, NULL); //DMA data xfer } how would I pass the offsetted buff address in the while loop? (i.e. buff + numBytesRead) Since, every time I got into the loop it will be buff then buff + numBytesRead then buff + numBytesRead + numBytesRead, ... etc. can I: #define BUFF_SIZE = 0X400; ULONG numBytesRead = 0, offset = 0; UCHAR *buff = null; buff = new UCHAR[45000000]; while(...) { ReadFile(hndlPciAlt, (buff + offset), BUFF_SIZE, numBytesRead, NULL); //DMA data xfer offset += numBytesRead; } Is (buff + offset) the proper syntax to increment the address of buff? Thanks. :-D

                  Kitty5

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

                  kitty5 wrote:

                  Is (buff + offset) the proper syntax to increment the address of buff?

                  Yes, that's how you need to do it. And your code snippet is correct also. You can stop the loop when offset == the number of bytes you need to read.


                  Cédric Moonen Software developer
                  Charting control

                  K 1 Reply Last reply
                  0
                  • C Cedric Moonen

                    kitty5 wrote:

                    Is (buff + offset) the proper syntax to increment the address of buff?

                    Yes, that's how you need to do it. And your code snippet is correct also. You can stop the loop when offset == the number of bytes you need to read.


                    Cédric Moonen Software developer
                    Charting control

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

                    Thank you so much for helping me clear this all up! :-D Things are so much clearer after a couple cups of coffee :laugh:....

                    Kitty5

                    1 Reply Last reply
                    0
                    • K kitty5

                      if I do: #DEFINE BUFF_SIZE 0x400; //# of bytes to read ULONG *buff = null, numBytesRead = 0; buff = new ULONG[11250000]; //total of 11,250,000 32-bit words Handle hdl = INVALID_HANDLE_VALUE; ... while(...){ ReadFile(hndlPciAlt, buff, BUFF_SIZE, numBytesRead, NULL); //DMA data xfer ... } **Where:** BOOl ReadFile( HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped ); is a windows function. Questions: 1) So, I pass in the *buff. ReadFile() will dump the data there. If I do another ReadFile() will it append to the end of *buff? OR, will it overlap the data already in the *buff, if so, how do I make subsequent writes into *buff append to the end of it? 2) Does ReadFile() automatically stick each 32-bit word out of the 1024 words it reads into each of the individual array slot of buff? So, that there will be a total of 256 array elements after the 1st call to ReadFile(). 3) If "DWORD nNumberOfBytesToRead" is the min number of bytes to read, will ReadFile() read more bytes if they are available? If so, how can you limit ReadFile to only read the specified number of bytes?

                      Kitty5

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

                      kitty5 wrote:

                      buff = new ULONG[11250000]; //total of 11,250,000 32-bit words

                      Just an FYI: Avoid declaring such large chucks of memory (be it stack or heap). While this array alone (which will be about 45 MB) won't cripple your system, imagine declaring 10 or 20 of them (throughout your application). Next thing you know, your application requires a full GB of RAM by itself (not good!). If possible, you should try to read a large file in smaller chunks, process the data, and read the next chuck.

                      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