dynamic array allocation

R

Rave

Is there a better way to do this in C++ ?

linkedlist * sort_array[2][MAX];

for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}


I tried but failed:

sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


Tks.
 
A

Attila Feher

Rave said:
Is there a better way to do this in C++ ?

linkedlist * sort_array[2][MAX];

sort_array is an array of an array of linkedlist pointers.
for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}


I tried but failed:

sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


You ask here to allocate an array of linkedlist pointers and then assing it
to another array. Arrays cannot be assigned in C++. What do you want to
do?
 
F

Frank Schmitt

Rave said:
Is there a better way to do this in C++ ?

linkedlist * sort_array[2][MAX];

for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}


I tried but failed:

sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


Use std::vector instead of arrays.

HTH & kind regards
frank
 
C

Chris Theis

Rave said:
Is there a better way to do this in C++ ?

linkedlist * sort_array[2][MAX];

for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}


I tried but failed:

sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


Tks.


Could you clarify what exactly you want to do. If you're thinking about
multidimensional dynamic arrays then you might take a look at nested
vectors:

Just a little example to give you an idea.

int i, j, cMax = 3;
vector< vector< vector<CObj> > > MyArr;
for( i = 0; i < 2; ++i ) {
MyArr.push_back( vector< vector<CObj> > () );
for( j = 0; j < cMax; j++ ) {
MyArr.push_back( vector<CObj>() );
}
}

for( i = 0; i < 10; i++ )
MyArr[0][0].push_back( CObj() );

Chris
 
R

Rave

I just want to allocate a 2-D array where each array element is a ptr to a
class called "linkedlist".

I know this works in C++ :
linkedlist * sort_array[2][MAX];

for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}
,
and this works in C but not C++ (using struct instead of class linkedlist) :

sort_array[0] = (struct item * *)malloc(sizeof(struct item *) * MAX);
sort_array[1] = (struct item * *)malloc(sizeof(struct item *) * MAX);

I am curious why the above doesn't work for C++. "Lvalue required" error.
Read about it but i am still not sure.

Tried this in C++ which should work but didn't :
sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


Regards.


Rave said:
Is there a better way to do this in C++ ?

linkedlist * sort_array[2][MAX];

for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}


I tried but failed:

sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];


Tks.
 
K

Kevin Goodsell

Rave said:
I just want to allocate a 2-D array where each array element is a ptr to a
class called "linkedlist".

Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/
I know this works in C++ :
linkedlist * sort_array[2][MAX];

This is the only declaration of sort_array you give. Are you saying you
want to do it differently? This alone meets your stated requirements.
for(int i=0; i<MAX; i++)
{
sort_array[0] = new (linkedlist);
sort_array[1] = new (linkedlist);
}
,
and this works in C but not C++ (using struct instead of class linkedlist) :

sort_array[0] = (struct item * *)malloc(sizeof(struct item *) * MAX);
sort_array[1] = (struct item * *)malloc(sizeof(struct item *) * MAX);


This doesn't work in C or C++ (given the earlier declaration of
sort_array). Arrays are non-modifiable lvalues, so you may not assign to
them.
I am curious why the above doesn't work for C++. "Lvalue required" error.
Read about it but i am still not sure.

Tried this in C++ which should work but didn't :
sort_array[0] = new (linkedlist*)[MAX];
sort_array[1] = new (linkedlist*)[MAX];

Again, because arrays may not be assigned to.

I hate guessing what people actually meant to ask, but if by chance you
wanted to dynamically allocate your arrays so that MAX does not need to
be a compile-time constant, you would do that this way:

linkedlist ** sort_array[2];

sort_array[0] = new linkedlist*[MAX];
sort_array[1] = new linkedlist*[MAX];

But it's almost always better (easier and safer) to use std::vector
rather than managing your own dynamic arrays:

vector<vector<linkedlist*> > sort_array;
// one way to get the size you want:
sort_array.resize(2, vector<linkedlist*>(MAX));

Or even:

vector<linkedlist*> sort_array[2];
sort_array[0].resize(MAX);
sort_array[1].resize(MAX);

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top