pointers and array of pointers question
-
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
-
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
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
-
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
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
-
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
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] -
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
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 ?
-
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
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 tobuff[0]
. If you advance that address by 2048 bytes, thedelete
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
-
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 tobuff[0]
. If you advance that address by 2048 bytes, thedelete
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
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
-
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
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
-
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
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 whichnew
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