Skip to main content
gamedev.net
🔒 Locked

dynamic sized array

Started by TheMatrixXXX Dec 15, 2002 at 4:46 PM 16 replies 600+ views
Original Post
TheMatrixXXX
TheMatrixXXX
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]]=--
--=[[TheMatrixXXX]]=--
AndyOxfeld
AndyOxfeld
No, it''s not. However, you can do the following, which is slightly more complicated:


#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
barazor
barazor
you could use a std::vector

for example:

  #include <vector>

vector<int> array;
array.reserve(20); // make it hold at least 20 items

array[0] = 343;
array[1] = 499;

array.push_back(34) // add another element to the end of the array

xg0blin
xg0blin
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.
TheMatrixXXX
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?



--=[[TheMatrixXXX]]=--
--=[[TheMatrixXXX]]=--
vtwin
vtwin
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
AndyOxfeld
AndyOxfeld
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
DrPizza
DrPizza
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
smart_idiot
smart_idiot
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]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
DerekSaw
DerekSaw
quote:

No, the beauty of the realloc() function is that you do not have to copy the entries back


Perhaps it is done internally?

Just use std::vector or std::deque to reduce headache on these.

[edited by - DerekSaw on December 16, 2002 1:42:09 AM]
"after many years of singularity, i'm still searching n the event horizon" o
risingdragon3
risingdragon3
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.
DrPizza
DrPizza
quote:
It is sad that C++ doesn''t have a realloc

No it isn''t -- there is no need for one. C only needs one because it''s so desperately lacking in expressive power -- it has no way to write a vector class. C++ has a vector class, so it doesn''t need realloc().
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
niyaw
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?


[edited by - niyaw on December 16, 2002 2:32:32 AM]
TheMatrixXXX
TheMatrixXXX
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]]=--
--=[[TheMatrixXXX]]=--
DrPizza
DrPizza
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
SabreMan
SabreMan
quote:
Original post by risingdragon3
It is sad that C++ doesn''t have a realloc

Gosh... that''s a strange comment. C++ *does* have a realloc. Amazingly, it''s called "realloc"!
TheMatrixXXX
TheMatrixXXX
whats the advantage of using the vector stuff?
i tried the realloc method and it works fine. thx again.



--=[[TheMatrixXXX]]=--
--=[[TheMatrixXXX]]=--
SabreMan
SabreMan
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.