dynamic array of ptrs to structs

Started by
5 comments, last by Convict@Large 22 years, 1 month ago
I need to store data on connected clients in a array of structs (easy enough), I only allow up to a Max number of clients to connect but I am a bit confused as to how to make it dyamic (using new operator), as I want to be able to change max clients at run-time and be able to change length of array used to store info. I am getting a bit confused does this code look right?
  
//  define test struct

struct STest
{
  int TInt;
}
//define ptr type for STest

typedef STest* ptr_STest;
// ptr to be used for array

STest *ArrayPtrTest;
/*this is where I get confused, will this give me an array of ptr to struct STest? */
ArrayPtrTest = new ptr_STest [100];
  
or should ArrayPtrTest be Declared as:
  
ptr_STest *ArrayPtrTest;
  
Cheers, Convict@Large
DanielB - Slayer of Bugs
Advertisement
100 pointers ....

What you wanna do is...

ArrayPtrTest = new STest [100];

...swisch and you have a pointer pointing to 100 STests.



And i think you should declare your pointer like this...

ptr_STest ArrayPtrTest;

...since you made a new type for it. But both ways work fine.

/Peter
Thanks for the quick response Peter.

Cheers,

Convict@Large
DanielB - Slayer of Bugs
std::vector vect_test;

Forget about arrays in C++ - they are plain daft!
A vector is a dynamic array isn''t it? I am wanting an array that I can declare the maximum size of, so a vector would be no use unless its size could be capped. Would a list be a better solution? Or a map as that would enforce unique connection ids which a list would not...

While we are on topic of STL (something I have yet to learn properally) can someone point me towards a good book or web tutoral for learning STL? I allready know C/C++ but have no experience using STL...

Cheers,

Convict@Large
DanielB - Slayer of Bugs
Why not just do a check, before adding new items? Something like...


  if (my_list.size () < 100){    my_list.push_back (item);}  


On a side note, I quite dislike the new code boxes.
Well yes, a vector can grow to meet the number of elements you insert. If you want to restrict the max number, then add some code to do the check, as siaspete suggests. However, you have to check the upper limit with an array too. The difference with an array is that you get undefined behaviour if you go over the limit. To my mind, that''s much worse!

As for an STL reference, I''ve yet to find anything that even gets close to Josuttis'' The C++ Standard Library.

quote:Original post by siaspete
On a side note, I quite dislike the new code boxes.

Me too.

This topic is closed to new replies.

Advertisement