dynamic struct arrays
-
normally when i wanted a struct array i would do something like this
typedef struct _TEST
{
char szString[64];
int number;
} TEST;TEST *t = (TEST*)malloc(sizeof(TEST) * 10));
in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?
-
normally when i wanted a struct array i would do something like this
typedef struct _TEST
{
char szString[64];
int number;
} TEST;TEST *t = (TEST*)malloc(sizeof(TEST) * 10));
in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?
check realloc()
-
normally when i wanted a struct array i would do something like this
typedef struct _TEST
{
char szString[64];
int number;
} TEST;TEST *t = (TEST*)malloc(sizeof(TEST) * 10));
in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?
The easiest way is to use
std::vector
.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
check realloc()
-
normally when i wanted a struct array i would do something like this
typedef struct _TEST
{
char szString[64];
int number;
} TEST;TEST *t = (TEST*)malloc(sizeof(TEST) * 10));
in which case i would end up with 10 elements in the struct array but say later, for example when a user adds an item to a list, i need to add another element, is there is there a way to easily do this ?
Yes it is. However, you need a totally new approach for this. Use a linked list. make a struct for the node like so: struct node { variable; . . . struct node *Link; }; now make a struct for the linked list like so struct list { node *First //first node in the list. void InsertNode(node *temp) //inserting at end of list, or anywhwere u like }; the first node will point to the next node, it will intern point to the next and so on. u can now use malloc to dynamically allocate mem for each node and append them at the end of the chain. remember to always keep the Link of teh last node in the chain as NULL. this is the end of the list. Please see more info on linked lists. There is too much out there for me to explain here, but this is the general format of a SINGLY LINKED LIST, but there many types. hope this has helped. you can change the functions in the list struct as per your requirements. you may add function for deleting, etc. You cannot create a dynamica array and maintain the data in it at the same time, unless you are using vb, where you can actually do this by ReDim. I hope i have answered your question:))
-
Yes it is. However, you need a totally new approach for this. Use a linked list. make a struct for the node like so: struct node { variable; . . . struct node *Link; }; now make a struct for the linked list like so struct list { node *First //first node in the list. void InsertNode(node *temp) //inserting at end of list, or anywhwere u like }; the first node will point to the next node, it will intern point to the next and so on. u can now use malloc to dynamically allocate mem for each node and append them at the end of the chain. remember to always keep the Link of teh last node in the chain as NULL. this is the end of the list. Please see more info on linked lists. There is too much out there for me to explain here, but this is the general format of a SINGLY LINKED LIST, but there many types. hope this has helped. you can change the functions in the list struct as per your requirements. you may add function for deleting, etc. You cannot create a dynamica array and maintain the data in it at the same time, unless you are using vb, where you can actually do this by ReDim. I hope i have answered your question:))
-
The easiest way is to use
std::vector
.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" - mYkel - 21 Jun '04 "There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05 Within you lies the power for good - Use it!
-
actually it seems to work just fine the way i was doing it, and no offense, but i would like a second opinion, before doing this the way you have suggested
Archer282 wrote: but i would like a second opinion, before doing this the way you have suggested Archer, I had this very same question. With the help of a couple of others I started using std::vector. It works very well. Here is the thread I started Data Storage[^] Here is an article explaining Vectors[^] Hope this helps, Good Luck... ------------------------------- DEBUGGING : Removing the needles from the haystack.
-
check realloc()
See my comment about
realloc()
here.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown