Skip to main content
gamedev.net
🔒 Locked

allocating to a dynamic array

Started by Basiror Apr 15, 2002 at 12:19 PM 2 replies 200+ views
Original Post
Basiror
Basiror
hello i just wondered if it is possible to allocate another particle to my dynamic array i have something like this list *pList=NULL pList=(list*)malloc(arraysize*sizeof(list)); so i have an array of pList[0]->pList[arraysize-1]; how can i add another arrayelement to pList that way? oldarraysize=arraysize; arraysize++; pList[arraysize-1]=(list*)malloc(sizeof(list)); so i have one more element would this work or do i have to set the ammount of elements i need when i allocate the memory at the start up? thx for helping
http://www.8ung.at/basiror/theironcross.html
krikkit
krikkit
If you need to have dynamic lists, use the STL.
Arrays, in and of themselves, have to be re-declared, re-allocated and copied if you want to size them. Coulda sworn this topic was active somewhere else on-site...
dalleboy
dalleboy
Or you could use realloc.

list *pList=NULL
size_t arraysize = 5;

pList = (list*) malloc(arraysize * sizeof(list));

pList is now containing 5 elements:
pList[0] ... pList[4];

arraysize = 10;
pList = (list*) realloc(pList, arraysize * sizeof(list));

pList is now containing 10 elements:
pList[0] ... pList[9];

Though I prefer STL, but STL are written in C++.

[edited by - dalleboy on April 15, 2002 1:32:04 PM]
Basiror
Basiror
thx for the quick reply i will use the realloc i prefer the old c++ functions
i don t want to switch my c++ style within one project to keep it readable
http://www.8ung.at/basiror/theironcross.html

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.