LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   dynamically expanding an array in c++ (https://www.linuxquestions.org/questions/programming-9/dynamically-expanding-an-array-in-c-154667/)

a1ghagh05t 03-07-2004 02:32 PM

dynamically expanding an array in c++
 
Hey,
I've got this pointer that I want to point to an array of pointers of length X. This array may get full so I may want to create a new one of length 2 * X and copy all the stuff from the original one into this new one. I'm doing this in C++ and whenever I declare the array...
void* asdf[ X ];
I get an error complaining that X isn't constant. So, I've been trying to work the "const" in there but I still can't get through this one. Thanks,
mj

jbstew32 03-07-2004 03:19 PM

Have you tried looking at vectors? I don't know how common they are in C++ or if there are better alternatives, but it holds a resize() function that seems to do all the work for you.

meldar 03-07-2004 03:22 PM

Look at http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c4027/

Stack 03-07-2004 04:11 PM

you need to use "new"

haobaba1 03-07-2004 09:00 PM

Re: dynamically expanding an array in c++
 
Quote:

Originally posted by a1ghagh05t
Hey,
I've got this pointer that I want to point to an array of pointers of length X. This array may get full so I may want to create a new one of length 2 * X and copy all the stuff from the original one into this new one. I'm doing this in C++ and whenever I declare the array...
void* asdf[ X ];
I get an error complaining that X isn't constant. So, I've been trying to work the "const" in there but I still can't get through this one. Thanks,
mj

void** asdf= new *void[x];

or you can write in c and compile with gcc which allows non-constant array declarations.


All times are GMT -5. The time now is 07:09 AM.