PDA

Click to See Complete Forum and Search --> : redim arrays?


Koya
Feb 20th, 2001, 10:52 PM
how do you redim an array in c++?

how do you change its size?

i mean, if you have:

int names[10];

can you change the '10' later on in your code to whatever you want?

please answer :(

Blitz
Feb 20th, 2001, 11:06 PM
You can't redim an array like in VB. If you wish to dynamically allocate a array, you will need to use pointers and the malloc() function. Not too sure if malloc() is still used in C++ though.

Koya
Feb 20th, 2001, 11:09 PM
well...

so there is FOR sure no way around this at all?

also...
why can't you do this

cin>>howmany;

int names[howmany];

that is basically what i need. i need the user to specify instead of me doing that.

any way around this at all?

Blitz
Feb 20th, 2001, 11:31 PM
No, you can't do allocation of arrays in any form without using pointers.

Before starting, do you have knowledge of pointers in the first place? Or else you will have sometime understanding the codes.

Vlatko
Feb 21st, 2001, 04:56 AM
Try this:


int howmany;
cin>>howmany;

int *names= new int[howmany];
//then
names[0] = 23423;
names[1] = ..;
..

Blitz
Feb 21st, 2001, 08:51 AM
Do remember to use the "delete" keyword to remove the array from the heap after finish using it :)


delete [] names;


If I'm not wrong with the syntax......

rekcus
Feb 21st, 2001, 12:51 PM
Hey guys, ever thought of using a linked-list? Do you think it is possible?

parksie
Feb 21st, 2001, 02:12 PM
Linked lists are useful, but if you're only occasionally reallocating then their overhead is too high. However, if inserting into the middle of the list is frequently needed, then they're great. Check out the STL list template, or the slist template if you only need forward traversal of the list.

parksie
Feb 23rd, 2001, 06:37 AM
You don't usually need to do that, since it handles reallocation internally:

vector<int> viMyArray;

viMyArray.push_back(5);

Technocrat
Feb 23rd, 2001, 10:08 AM
Maybe some on could spend the time and make a dll, header file, or class to do redim, ubound, etc easily.
<All eyes turn to look at parksie>

parksie
Feb 23rd, 2001, 12:18 PM
*menacing*
You talkin' ta me?
*growls*

UBound is nasty to do in C++ since you don't know which dimension you're using. You need to divide the dimension size by the size of it's parent (I think...this may be the wrong way around!)

LBound is easy:

inline long LBound(void *pArray) { return 0; }

:p:p:p:p:p:p:p:p
Hehehehehe

ReDim, well, normally you'd just delete and reallocate. Preserve is a little more complicated, but dead simple. Unfortunately they're pretty much specific to the situation...templates anyone?

*looks at Technocrat*

Double Dare time :D

Technocrat
Feb 23rd, 2001, 12:29 PM
ooooooooohhhhhhhhhh, rumble in the C++ jungle. ;)

I would try it if I had time. In fact I might when I get done with my current project. It would be nice to have those types of abilities that were built into arrays in VB. Redim, ubound, lbound were so handy.

Technocrat
Feb 23rd, 2001, 12:41 PM
Oh and dont make me get my pillow case of oranges!

:)