LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2004, 02:32 PM   #1
a1ghagh05t
LQ Newbie
 
Registered: Jan 2004
Distribution: red hat 9
Posts: 3

Rep: Reputation: 0
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
 
Old 03-07-2004, 03:19 PM   #2
jbstew32
Member
 
Registered: Feb 2003
Location: Atlanta, GA
Posts: 151

Rep: Reputation: 15
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.
 
Old 03-07-2004, 03:22 PM   #3
meldar
Member
 
Registered: Feb 2004
Location: Denmark
Distribution: Gentoo
Posts: 136

Rep: Reputation: 15
Look at http://www.codeguru.com/Cpp/Cpp/cpp_...cle.php/c4027/
 
Old 03-07-2004, 04:11 PM   #4
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
you need to use "new"
 
Old 03-07-2004, 09:00 PM   #5
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
In C, using qsort for dynamically allocated array ntmsz Programming 7 08-23-2005 10:33 AM
expanding dynamic array niteshadw Programming 6 06-25-2005 11:19 PM
expanding apt ripmaster Ubuntu 2 05-15-2005 12:16 AM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM
expanding / michael_mead Linux - General 3 09-03-2003 02:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration