|
-
Sep 19th, 2001, 03:59 AM
#1
Thread Starter
Lively Member
Arrays on the free store
Hey Guys
I was just wondering if there is anyway to increase the size of an array on the freestore after it has been declared.
For instance
i have
BYTE *pArray
pArray = new BYTE[12];
now i want to increase the size of the array to 13 or 14 here
Cheers Guys
Peter
"Let's all join forces, rule with an iron hand...and prove to all the world, metal rules the land..."
-- Judas Priest
My email is [email protected]
-
Sep 19th, 2001, 04:55 AM
#2
Frenzied Member
Well using the old C-style memory allocation functions, you could use malloc() to allocate and realloc() to resize allocated memory. I guess there must be something similar for the new and delete operators, but I really don't know what.
There is a placement option in the syntax of the new operator, but that's only used in user-defined new operators (used in operator new() functions in classes you want to handle memory allocation for yourself).
You might be better off doing one of the following:
- use malloc(), realloc() and free() for dynamic memory allocation
- make a wrapper class for your array and implement new and delete yourself
- don't use an array, use the STL vector class
Of these, the easiest will probably be to use the STL vector class. It's like a normal array but it's resizeable and some other cool stuff.
Harry.
"From one thing, know ten thousand things."
-
Sep 19th, 2001, 10:20 AM
#3
Code:
// C
BYTE* array;
array = (BYTE*)malloc(10 * sizeof(BYTE));
// resize to 12
BYTE* temparray = array;
array = (BYTE*)malloc(12 * sizeof(BYTE));
memcpy(array, temparray, 10 * sizeof(BYTE));
free(temparray);
// C++
BYTE* array = new BYTE[10];
// resize
BYTE* temparray = array;
array = new BYTE[12];
memcpy(array, temparray, 10 * sizeof(BYTE));
delete[] temparray;
This is it.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 19th, 2001, 11:43 AM
#4
Frenzied Member
Well I wouldn't say that's "it". That's one way of getting around the problem, which is similar to the way the STL vector class achieves it. It's not the same as resizing the existing array.
Still, it does achieve the same result Check out the vector class if you want to do this kind of thing a lot though, this kind of thing is the whole point in its existence.
Harry.
"From one thing, know ten thousand things."
-
Sep 19th, 2001, 04:05 PM
#5
Monday Morning Lunatic
Actually, the vector is more efficient because it only reallocates memory in blocks of some integer multiple of the element size.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 20th, 2001, 03:54 AM
#6
transcendental analytic
Originally posted by parksie
Actually, the vector is more efficient because it only reallocates memory in blocks of some integer multiple of the element size.
HOW does it do that?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 20th, 2001, 11:12 AM
#7
Monday Morning Lunatic
The vector is initialised, and it's empty. You add one more item to it using push_back. It then allocates enough space for 5 items (may be some other value, I think you can choose in the constructor or template arguments), and sets the reserved size to 5. However, the USED size is only 1.
It copies your data into the first element in the buffer.
When you come to add another element, it knows it has space so it merely adds them to the memory it already has. If it runs out (already got 5 items) then it allocates more memory (with the excess of 5) and copies the data in, appending the new item.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 20th, 2001, 11:18 AM
#8
transcendental analytic
Originally posted by parksie
If it runs out (already got 5 items) then it allocates more memory (with the excess of 5) and copies the data in, appending the new item.
I knew it was something tricky! Hehe, Can't trust any vectors anymore, they take away all your memory
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|