reduce size of array
-
Andrew Kirillov wrote: You can try to use realloc(lpBuffer, new_size) No! The original buffer was allocated with new.
Sloppy Joseph wrote: No! The original buffer was allocated with new. So ? Does not the
new
equal tomalloc()
, anddelete
equal tofree()
? Andrew -
Sloppy Joseph wrote: No! The original buffer was allocated with new. So ? Does not the
new
equal tomalloc()
, anddelete
equal tofree()
? Andrew -
i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho
you could use
list<>
orvector<>
. otherwise, the only way is to create a new array (using new operator, the length you like), then copy the datas you want to preserve during the "resize operation", thendelete
the old array...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Andrew Kirillov wrote: Does not the new equal to malloc(), and delete equal to free() ? no, not at all !!! new and delete are re-entring functions, while C ones (malloc, realloc, free) aren't.
TOXCCT >>> GEII power
[toxcct][VisualCalc]Yes, you are right. I found the answer. I just was confused by a small sample using MFC - it was looking that they are the same. But they are not. Thanx ! Andrew
-
i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho
If possible, use stack memory instead of allocating. Then you don't have to care about size. Don't try it, just do it! ;-)
-
i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho
Why do you want to decrease the size of the buffer? You know that there are no easy ways to manipulate the size of an array! The only way to change the size of an array is to create a complete new one with the size you want and copy all the items you can from the old array.. As other users pointed out, you can use a
std::vector
or some other collection class. Behind every great black man... ... is the police. - Conspiracy brother Blog[^] -
i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho
Something like:
LPBYTE lpBuffer2 = new BYTE[1000];
memcpy(lpBuffer2, lpBuffer, 2 * sizeof(BYTE));
delete [] lpBuffer2;The other way is to not make it too big in the first place. Lastly, even if you ended up just using 2 of the 5000 bytes, that's not a big deal in the overall scheme of things.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Andrew Kirillov wrote: You can try to use realloc(lpBuffer, new_size) No! The original buffer was allocated with new.
-
Something like:
LPBYTE lpBuffer2 = new BYTE[1000];
memcpy(lpBuffer2, lpBuffer, 2 * sizeof(BYTE));
delete [] lpBuffer2;The other way is to not make it too big in the first place. Lastly, even if you ended up just using 2 of the 5000 bytes, that's not a big deal in the overall scheme of things.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
Strange. Declare and array of 1000 bytes, then copy 2 bytes from the old array to the new array.:doh: INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
Strange. Declare and array of 1000 bytes, then copy 2 bytes from the old array to the new array.:doh: INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
John R. Shaw wrote: Strange. Indeed, but since the OP did not specify what size he wanted the array "shrunk" to, I just pulled a smaller number from the sky.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
John R. Shaw wrote: Strange. Indeed, but since the OP did not specify what size he wanted the array "shrunk" to, I just pulled a smaller number from the sky.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
:-DYeh; I thought about that after the fact. Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
:-DYeh; I thought about that after the fact. Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
John R. Shaw wrote: Normaly I just substitute the numbers with something like new_array_size. Because it is closer to how we would actually write it. Yes, I've done that too, only to be asked shortly afterwards, "What is this new_array_size thing and how do I use it?"
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
i created a buffer, how do i reduce the size of lpBuffer without losing the initial items in it. LPBYTE lpBuffer; lpBuffer = new BYTE[5000]; lpBuffer[0] = 'h'; lpBuffer[1] = 'i'; easiest way to make lpBuffer smaller size?! Any help is appreciated thanks! Mepho
Ya first of all just like to thank everyone for helping out cant use Cbytes for the application and ya the buffer doesnt get filled by only 2... its filled by user specified items. So ill probably use the memcpy function that DavidCrow suggested, the buffer will be reduced by checking the amount the user filled. Was hoping i didnt need to create another buffer array but seems like best solution is to cpy it so far. Thanks, Mepho