🔒 Locked
dynamic sized array
Advertisement
Original Post
when i create an array like:
int Array[10];
now when i need one more int at runtime, is it possible to increase the array by one item without re-create it?.
thx
--=[[TheMatrixXXX]]=--
No, it''s not. However, you can do the following, which is slightly more complicated:
And then, to increase it from 10 (NUMITEMS) to 11 (NUMITEMS + 1), you just do the following:
Then, at the bottom of the function, you MUST call this function, or you will have memory leaks:
Hope that helps.
- Andy Oxfeld
#define NUMITEMS 10
int* Array;
Array = (int*)malloc(NUMITEMS * sizeof(int));
And then, to increase it from 10 (NUMITEMS) to 11 (NUMITEMS + 1), you just do the following:
Array = (int*)realloc(Array, (NUMITEMS + 1) * sizeof(int));
Then, at the bottom of the function, you MUST call this function, or you will have memory leaks:
free(Array);
Hope that helps.
- Andy Oxfeld
Look into stl::vector. It is basically a dynamic array. All you have to do is include #include <vector> and then make a vector of whatever data type you like. example vector MyVect;
You can then resize, push items onto the vector, pop items off of the vector, and you can still acess it the same way as an array like MyVect[index]. They also work well for two and three dimensional arrays. soemthing like vector >Myvect;
THen you can do MyVect[index][index]. If you just have to use regular arrays though, you can look into realloc. If the size has to be dynamic you could just use the new operator (or malloc if you want to use c) rather than just making an array of n items.
You can then resize, push items onto the vector, pop items off of the vector, and you can still acess it the same way as an array like MyVect[index]. They also work well for two and three dimensional arrays. soemthing like vector
THen you can do MyVect[index][index]. If you just have to use regular arrays though, you can look into realloc. If the size has to be dynamic you could just use the new operator (or malloc if you want to use c) rather than just making an array of n items.
so i always must create a temp are with the same size copy all entries to the temp, re-create the original with size+1 and copy all back?
--=[[TheMatrixXXX]]=--
--=[[TheMatrixXXX]]=--
quote:
Original post by TheMatrixXXX
so i always must create a temp are with the same size copy all entries to the temp, re-create the original with size+1 and copy all back?
Urm..no..
Have you not read a single word the previous posters have said?
swift URL | browse my bookmarks [server may not always be on]
Sensimed | Info/download
quote:
so i always must create a temp are with the same size copy all entries to the temp, re-create the original with size+1 and copy all back?
No, the beauty of the realloc() function is that you do not have to copy the entries back.
Just call malloc() to allocate the initial size (10). Then, when you want to raise it to 11, just call realloc. After calling realloc, your original 10 items will still be there, intact, and you will then have room for 1 more.
- Andy Oxfeld
quote:
Original post by AndyOxfeld
No, it''s not. However, you can do the following, which is slightly more complicated:
Hope that helps.
- Andy Oxfeld
Why are you casting the return values of malloc() and realloc()?
If you are using C, casting the return values of malloc() and realloc() usually hides certain warnings indicative of programming errors. The language doesn''t need it, because of its implicit cast from void* to pointer types rule.
If you are using C++ (which doesn''t perform implicit casts from void* and hence would need the cast), you should not be using malloc() or realloc() in the first place, but instead std::vector.
If you''re using a language in which you should use those functions, you don''t need the cast; if you''re using a language in which you need the cast, you shouldn''t be using those functions.
quote:
The realloc function changes the size of the block whose address is ptr to be newsize.
Since the space after the end of the block may be in use, realloc may find it necessary to copy the block to a new address where more free space is available. The value of realloc is the new address of the block. If the block needs to be moved, realloc copies the old contents.
Can't do that with new/delete. Of course it doesn't need to do that, and I haven't tested if it will actually not move the data if it doesn't need to, but it still sounds cool.
Of course std::vector is type safe and constructors/destructors will be called and it's exception safe and other good things like that, but then you don't get to try out those cool things like static_cast and reinterpret_cast that you only read about in text books.
[edited by - smart_idiot on December 15, 2002 12:56:36 AM]
quote:
Perhaps it is done internally?
It depends. Realloc looks through the heap and finds the memory block that corresponds to the memory that is passed in. If there is enough memory after the the block to expand in, then realloc doesn''t copy / destroy / create. If there isn''t enough space, then it will expand without destroy.
Note. My language makes it seem that there is a search involved in realloc, but there isn''t. The memory usually has a structure that tags the size of the block...like...
struct mem {
int size;
char* mem;
};
so it''s really just one dereferencee. But anyway...
that''s compiler land.
It is sad that C++ doesn''t have a realloc, but there are ways to ''fake'' it with C++ code (cuj.com, C++ experts forum - I forget which).
-----------------------------
Gamedev for learning.
libGDN for putting it all together.
thx for your replies, but i´ve never worked with this vector stuff so i don´t know how to use them.
i will try to solve my problem with using the realloc() function.
@vtwin: i´ve started writing my answer when there was only one reply, but i had to do different stuff while i was writing so i saw the other replies AFTER i posted my second message !!
if you have nothing better to do than criticize other people instead giving a answer to the question THEN BE QUITE OK ?!
--=[[TheMatrixXXX]]=--
i will try to solve my problem with using the realloc() function.
@vtwin: i´ve started writing my answer when there was only one reply, but i had to do different stuff while i was writing so i saw the other replies AFTER i posted my second message !!
if you have nothing better to do than criticize other people instead giving a answer to the question THEN BE QUITE OK ?!
--=[[TheMatrixXXX]]=--
quote:
Original post by niyaw
but wouldn''t it be nice if vector could call realloc and grow in-place? vectors perform memory management separately from construction anyway (ie, they do placement new and explicit destructor calls), so why not use realloc?
But vectors /do/ grow in place, because they overallocate, which makes realloc() significantly less important.
whats the advantage of using the vector stuff?
i tried the realloc method and it works fine. thx again.
--=[[TheMatrixXXX]]=--
i tried the realloc method and it works fine. thx again.
--=[[TheMatrixXXX]]=--
quote:
Original post by TheMatrixXXX
whats the advantage of using the vector stuff?
std::vector is a fully-formed Abstract Data Type, which means that it fits neatly and naturally into C++, and it has all sorts of capabilities above and beyond arrays such as it will correctly hold objects with no effort on the part of the user. It is also not subject to most of the idiocy of C-style arrays.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement